Selection and additional sorts
These methods use either stronger structure than arbitrary comparisons or a more selective output requirement. State whether keys are distinct, whether arithmetic is constant time, and which random model justifies an expectation.
3.1 Comparison lower bounds
Past papers · CS 2014–2026 (23 papers): 3 questions, 4 marks
A comparison decision tree must distinguish all relevant outcomes. Finding any element other than the maximum is easier than finding the minimum: comparing two distinct elements already provides a nonmaximum.
For , a balanced tournament finds the second largest in worst-case comparisons: find the largest, then the largest among the keys that lost directly to it. This is not a need to sort the whole array.
3.2 Order statistics and selection
Past papers · CS 2014–2026 (23 papers): 3 questions, 5 marks
The th smallest uses one-based rank . Partitioning can discard the side that cannot contain that rank, unlike quicksort, which must sort both sides.
Median-of-medians groups keys in fives, recursively selects the median of the group medians, and partitions. Apart from a constant number of boundary keys, at least keys lie on each side. Thus . Reading arbitrary input gives the matching bound. Computing a median in each of arrays of length , then the median of those medians, costs ; it need not equal the median of all elements.
3.3 Heapsort
Past papers · CS 2014–2026 (23 papers): 1 question, 1 mark
Build a max-heap, repeatedly swap its maximum with the last active position, shrink the active heap and sift down. The ascending suffix is final. Heap representation and individual heap operations are in the Programming and Data Structures handbook.
3.4 Integer sorting
Comparison lower bounds do not apply when key values directly address buckets. A stable integer sort can also serve as one digit pass of radix sorting.
Counting sort: count each value, prefix-sum counts, then place input entries from right to left while decrementing their cumulative end positions. Least-significant-digit radix sorting must keep each pass stable. Bucket sort on independent uniform values in with equal buckets and insertion sort inside buckets has expected time and worst ; skewed distributions remove the expected guarantee.
Last-minute recall
- Arbitrary comparison sorting needs worst comparisons.
- Finding both extremes uses paired comparisons.
- Quickselect recurses on one part; quicksort on both.
- Bottom-up heap construction is linear.
- LSD radix passes must be stable.