Partial sorting

In computer science, partial sorting is a relaxed variant of the sorting problem. Total sorting is the problem of returning a list of items such that its elements all appear in order, while partial sorting is returning a list of the k smallest (or k largest) elements in order. The other elements (above the k smallest ones) may also be stored, as in an in-place partial sort, or may be discarded, which is common in streaming partial sorts. A common practical example of partial sorting is computing the "Top 100" of some list.

In terms of indices, in a partially sorted list, for every index i from 1 to k, the ith element is in the same place as it would be in the fully sorted list: element i of the partially sorted list contains order statistic i of the input list.

Solution by partitioning selection

A further relaxation requiring only a list of the k smallest elements, but without requiring that these be ordered, makes the problem equivalent to partition-based selection; the original partial sorting problem can be solved by such a selection algorithm to obtain an array where the first k elements are the k smallest, and sorting these, at a total expected cost of O(n + k log k) operations. When quickselect and quicksort are used as the building blocks in this algorithm, the result is called "quickselsort".[1]

Heap-based solutions

Binary heaps lead to an O(n + k log n) solution to partial sorting: partial heapsort. First "heapify", in linear time, the complete input array. Then extract the minimum of the heap k times.[1]

A streaming, single-pass partial sort is also possible using heaps or other priority queue data structures. First, insert the first k elements of the input into the structure. Then make one pass over the remaining elements, add each to the structure in turn, and remove the largest element. Each insertion operation also takes O(log k) time, resulting in O(n log k) time overall; this algorithm is practical for small values of k and in online settings.[1]

Specialised sorting algorithms

More efficient than any of these are specialized partial sorting algorithms based on mergesort and quicksort. In the quicksort variant, there is no need to recursively sort partitions which only contain elements that would fall after the k'th place in the final sorted array (starting from the "left" boundary). Thus, if the pivot falls in position k or later, we recurse only on the left partition:[2]

``` function partial_quicksort(A, i, j, k)
if i < j
p ← pivot(A, i, j)
p ← partition(A, i, j, p)
partial_quicksort(A, i, p-1, k)
if p < k-1
partial_quicksort(A, p+1, j, k)
```

The resulting algorithm is called partial quicksort and requires an expected time of only O(n + k log k), and is quite efficient in practice, especially if we substitute selection sort when k becomes small relative to n. However, the worst-case time complexity is still very bad, in the case of a bad pivot selection. Pivot selection along the lines of the worst-case linear time selection algorithm could be used to get better worst-case performance.

Tournament algorithm

Another method is the tournament algorithm. The idea is to conduct a knockout minimal round tournament to decide the ranks. It first organises the games (comparisons) between adjacent pairs and moves the winners to next round until championship (the first best) is decided. It also constructs the tournament tree along the way. Now the second best element must be among the direct losers to winner and these losers can be found out by walking in the binary tree in O(log n) time. It organises another tournament to decide the second best among these potential elements. The third best must be one among the losers of the second best in either of the two tournament trees. The approach continues until we find k elements. This algorithm takes O(n + k log n) complexity, which for any fixed k independent of n is O(n).

Language/library support

• The C++ standard specifies a library function called `std::partial_sort`.
• The Python standard library includes functions `nlargest` and `nsmallest` in its `heapq` module.

References

1. ^ a b c Conrado Martínez (2004). "On partial sorting". 10th Seminar on the Analysis of Algorithms.
2. ^ Martínez, Conrado (2004). "Partial quicksort". Proc. 6th ACM-SIAM Workshop on Algorithm Engineering and Experiments and 1st ACM-SIAM Workshop on Analytic Algorithmics and Combinatorics.