Questions: Selection Algorithms: Finding the kth Smallest Element
5 questions to test your understanding
Score: 0 / 5
Question 1 Multiple Choice
To find the median of 1 million unsorted numbers, a programmer sorts the array and returns the middle element. A colleague claims there is a faster approach for this specific task. Which statement best explains the improvement?
ASorting integers can be done in O(n) time, so the sort-based approach is already optimal
BQuickselect can find the kth element in O(n) average time by recursing into only the relevant partition after each pivot step, avoiding the work of sorting both halves
CBinary search on the unsorted array finds the median in O(log n) time
DMerge sort is faster than quicksort for median-finding because it has no worst-case degradation
Quickselect reuses quicksort's partition step but only recurses into the one partition that must contain the kth element — the other half is discarded. This means each recursive call processes roughly half the elements of the previous one, so total work is n + n/2 + n/4 + ... = O(n) on average. Sorting both halves (as quicksort does) costs O(n log n) — doing far more work than finding a single element requires.
Question 2 Multiple Choice
A software engineer needs the 90th percentile value in a large dataset where worst-case latency guarantees are not required. She chooses Median-of-Medians over randomized quickselect. Is this a good practical choice?
AYes — Median-of-Medians always outperforms quickselect because its worst case is O(n)
BYes — Median-of-Medians is the standard implementation in C++ std::nth_element and NumPy
CNo — Median-of-Medians has a significantly larger constant factor (roughly 5-10x) making randomized quickselect faster in practice, despite quickselect's theoretical O(n²) worst case
DNo — Median-of-Medians does not guarantee finding the correct kth element
Median-of-Medians guarantees O(n) worst-case time by using a careful pivot-selection procedure (medians of groups of five), but this procedure itself adds substantial constant overhead. Randomized quickselect has an astronomically unlikely O(n²) worst case but runs much faster on real data. Standard library implementations (C++ std::nth_element, NumPy numpy.partition) use quickselect variants precisely because practical speed matters more than worst-case guarantees for this problem.
Question 3 True / False
Quickselect finds the kth smallest element in O(n log n) time on average — the same as sorting — but uses less memory.
TTrue
FFalse
Answer: False
Quickselect achieves O(n) average time, not O(n log n). The key insight is single-sided recursion: after partitioning, quickselect only recurses into the partition containing the kth element. This means the work at successive levels forms a geometric series: n + n/2 + n/4 + ... ≈ 2n = O(n). Compare this to quicksort, which recurses into both halves at every level across O(log n) levels, giving O(n log n).
Question 4 True / False
Randomized pivot selection makes quickselect's worst-case behavior astronomically unlikely in practice, even though the theoretical worst case remains O(n²).
TTrue
FFalse
Answer: True
With a random pivot, the probability of repeatedly choosing a near-extreme element decreases exponentially with each partitioning step. While an adversarial input could force O(n²) on a fixed-pivot implementation, a randomized pivot makes this essentially impossible — no fixed input can cause worst-case behavior because the pivot selection is random. This is why randomized quickselect is described as having expected O(n) performance and is the practical choice over the theoretically superior but slower Median-of-Medians.
Question 5 Short Answer
What is the key algorithmic difference between quickselect and quicksort that allows quickselect to run in O(n) average time while quicksort requires O(n log n)?
Think about your answer, then reveal below.
Model answer: Both algorithms use the same partition step: pick a pivot, place elements smaller than it on the left and larger on the right, leaving the pivot at its final sorted position. Quicksort then recurses into both partitions to sort all elements. Quickselect recurses into only one partition — the side that must contain the kth element — and discards the other entirely. This single-sided recursion means each level processes roughly half the elements of the previous level: n + n/2 + n/4 + ... = O(n) total. Quicksort's two-sided recursion processes all n elements across O(log n) levels, yielding O(n log n).
The insight generalizes: whenever you only need one answer from a dataset (the kth element, the maximum, the minimum), you can often avoid the overhead of producing a fully sorted order. Selection is fundamentally easier than sorting, and quickselect exploits this by pruning the irrelevant half at each step rather than producing a complete sorted order.