Chapter 3

Selection and additional sortsCS

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 boundsCS

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 n=2q≥2n=2^q\ge2, a balanced tournament finds the second largest in n+log⁡2n−2n+\log_2 n-2 worst-case comparisons: find the largest, then the largest among the qq keys that lost directly to it. This is not a need to sort the whole array.

3.2 Order statistics and selectionCS

Past papers · CS 2014–2026 (23 papers): 3 questions, 5 marks

The rrth smallest uses one-based rank 1≤r≤n1\le r\le n. 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 3n/103n/10 keys lie on each side. Thus T(n)≤T(⌈n/5⌉)+T(7n/10+O(1))+O(n)=O(n)T(n)\le T(\lceil n/5\rceil)+T(7n/10+O(1))+O(n)=O(n). Reading arbitrary input gives the matching Ω(n)\Omega(n) bound. Computing a median in each of kk arrays of length nn, then the median of those medians, costs Θ(kn+k)\Theta(kn+k); it need not equal the median of all knkn elements.

3.3 HeapsortCS

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 sortingCS

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 [0,1)[0,1) with nn equal buckets and insertion sort inside buckets has expected Θ(n)\Theta(n) time and worst Θ(n2)\Theta(n^2); skewed distributions remove the expected guarantee.

Last-minute recall

  • Arbitrary comparison sorting needs Ω(nlog⁡n)\Omega(n\log n) 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.

Revise offline: the whole Algorithms handbook as one PDF

52 pages · 2.6 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections