On this page
You have n numbers and want the k-th smallest, say the median of a class's marks. Sorting first costs O(n log n). Selection algorithms do it in linear time.
What is quickselect and why is it expected O(n)?
Quickselect is quicksort with one branch cut off. It was developed by Tony Hoare, who published it as Algorithm 65, FIND, in Communications of the ACM in 1961.
Pick a pivot.
Partition: everything smaller goes left, everything larger goes right. Now the pivot sits at its true rank r.
If r = k, stop. If k < r, repeat on the left part. If k > r, repeat on the right part, looking for the (k − r)-th smallest.
With a random pivot the part you keep shrinks by a constant fraction on average, so the total work is a geometric series. Quickselect has expected linear running time and a quadratic worst case.
A worked run: the median of 11 numbers
Take these 11 numbers: 42, 17, 93, 8, 61, 25, 70, 3, 56, 34, 88. The median is the 6th smallest. Rule for this run: the pivot is the middle element of the current list, kept in its original order (the left middle one when the length is even).
List of 11, pivot 25. Smaller: 17, 8, 3. Larger: 42, 93, 61, 70, 56, 34, 88. The pivot's rank is 4. We need 6 > 4, so keep the larger part and look for its 2nd smallest. (10 comparisons)
List of 7, pivot 70. Smaller: 42, 61, 56, 34. Larger: 93, 88. Rank 5. We need 2, so keep the smaller part. (6 comparisons)
List 42, 61, 56, 34, pivot 61. Smaller: 42, 56, 34. Rank 4. Keep the smaller part, still looking for the 2nd. (3 comparisons)
List 42, 56, 34, pivot 56. Smaller: 42, 34. Rank 3. Keep the smaller part. (2 comparisons)
List 42, 34, pivot 42. Smaller: 34. Rank 2. Found it. (1 comparison)
The median is 42, after 22 comparisons. Check: sorted, it reads 3, 8, 17, 25, 34, 42, 56, 61, 70, 88, 93, and 42 is 6th.
When does quickselect go wrong?
The bad case is a pivot that removes only itself each time. Suppose you want the smallest of 11 numbers and your rule keeps picking the largest remaining number. Each round removes one element, so you make 10 + 9 + ... + 1 = 55 comparisons, which is n(n − 1)/2. That grows as n², and an adversary who knows your pivot rule can force it every time.
How does median of medians guarantee O(n)?
Median of medians divides the input into groups of at most five elements, takes each group's median, and recursively finds the median of those medians to use as the pivot. It was published by Blum, Floyd, Pratt, Rivest and Tarjan in 1973 in the Journal of Computer and System Sciences80033-9), which is why it is also called BFPRT.
Why is that pivot good? Picture the groups as columns of 5, each sorted, with the columns ordered by their medians (the hero diagram above). Half the columns have a median no bigger than the pivot, and in each of those the median and the two below it are no bigger than the pivot. That is 3 elements in each of about n/10 columns, so roughly 3n/10 elements are ≤ the pivot, and by the same argument roughly 3n/10 are ≥ it. Whichever side you keep has at most about 7n/10 elements: at least 30% of the elements are discarded at each step.
On our 11 numbers the groups are (42, 17, 93, 8, 61), (25, 70, 3, 56, 34) and (88), with medians 42, 34 and 88. Their median, 42, happens to be the exact answer. That is luck.
Why does T(n) ≤ T(n/5) + T(7n/10) + O(n) solve to O(n)?
Linear work for group medians and partitioning, plus recursive calls on the n/5 medians and on at most 7n/10 elements, gives the recurrence T(n) ≤ T(n/5) + T(7n/10) + cn.
The key fact is 1/5 + 7/10 = 9/10, which is less than 1. The work per level of recursion is at most cn, then 0.9cn, then 0.81cn and so on. That geometric series sums to at most 10cn, which is O(n).
The master theorem for divide-and-conquer recurrences does not apply directly, because the two subproblems have different sizes. For the notation itself, see Big O notation and growth rates.
How should you revise this?
Redo the 11-number run with the pivot rule "always the first element" and count comparisons.
Draw the grid for n = 35 and shade the cells you know are ≤ the pivot.
If recurrences or partitioning still feel shaky, the Fermi Concept Library covers every concept in computing and mathematics, and the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



