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 elements in order. The other elements may also be sorted, 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 i-th 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.
Offline problems
Heap-based solution
admit a simple single-pass partial sort when is fixed: insert the first elements of the input into a max-heap. Then make one pass over the remaining elements, add each to the heap in turn, and remove the largest element. Each insertion operation takes time, resulting in time overall; this "partial heapsort" algorithm is practical for small values of and in online settings. An "online heapselect" algorithm described below, based on a min-heap, takes.Solution by partitioning selection
A further relaxation requiring only a list of the 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 elements are the smallest, and sorting these, at a total cost of operations. A popular choice to implement this algorithm scheme is to combine quickselect and quicksort; the result is sometimes called "quickselsort".Common in current C++ STL implementations is a pass of heapselect for a list of k elements, followed by a heapsort for the final result.
Specialised sorting algorithms
More efficient than the aforementioned 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 'th place in the final sorted array. Thus, if the pivot falls in position or later, we recurse only on the left partition:function partial_quicksort is
if i < j then
p ← pivot
p ← partition
partial_quicksort
if p < k-1 then
partial_quicksort
The resulting algorithm is called partial quicksort and requires an expected time of only, and is quite efficient in practice, especially if a selection sort is used as a base case when becomes small relative to. 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. Partial quicksort, quickselect, and quicksort can all be generalized into what is known as a chunksort.
Incremental sorting
Incremental sorting is a version of the partial sorting problem where the input is given up front but is unknown: given a -sorted array, it should be possible to extend the partially sorted part so that the array becomes -sorted.Heaps lead to an "online heapselect" solution to incremental partial sorting: first "heapify", in linear time, the complete input array to produce a min-heap. Then extract the minimum of the heap times.
A different incremental sort can be obtained by modifying quickselect. The version due to Paredes and Navarro maintains a stack of pivots across calls, so that incremental sorting can be accomplished by repeatedly requesting the smallest item of an array from the following algorithm:
Algorithm returns the 'th smallest element in
- If :
- * Pop
- * Return
- Let
- Update
- Push onto
- Return
Language/library support
- The C++ standard specifies a library function called
. - The Python standard library includes functions
andin itsheapqmodule. - The Julia standard library includes a
algorithm used inand variants.