Linear-Time Selection: Median of Medians
The deterministic O(n) algorithm of Blum, Floyd, Pratt, Rivest and Tarjan.
In 1973, five researchers, four of whom would later receive the Turing Award, proved that the median of numbers can be found in linear time without any randomness. Their algorithm fits on half a page. How many comparisons the median really needs is a different story: somewhere between and , and nobody has closed the gap.
What you’ll learn
- State the median-of-medians algorithm (BFPRT) and prove that its pivot has at least elements on each side.
- Solve by substitution, and explain why groups of 3 fail and groups of 7 or more succeed.
- Prove a lower bound for the median with an adversary argument, and place it among the known bounds: below, above.
- Explain the role of deterministic selection in practice: a fallback that guarantees linear time when quickselect's pivots go wrong.
The algorithm and its 30/70 guarantee
Quickselect is fast on average, but a run of bad pivots makes it quadratic. Blum, Floyd, Pratt, Rivest and Tarjan (1973) removed the luck: they compute a pivot that is guaranteed to be near the middle, at a cost small enough to keep the total linear. The pivot is the median of medians, found by a recursive call of the algorithm itself.
SELECT(A, k): # the element of rank k (0-indexed); keys distinct
if |A| <= 5: return the element of rank k of sorted(A)
split A into groups of 5 (the last may be smaller)
M <- the median of each group # 6 comparisons per full group suffice
x <- SELECT(M, floor((|M| - 1) / 2)) # the median of the medians
L <- elements of A smaller than x
R <- elements of A larger than x
if k < |L|: return SELECT(L, k)
if k = |L|: return x
return SELECT(R, k - |L| - 1)Two recursive calls per round: one on the medians to choose the pivot, one on a single side of the partition. A Python transcription, tested against sorting on random inputs, never kept more than elements.
Let be the number of groups. Since is the median of the group medians, at least half of those medians are :
Each such group holds at least 3 elements : its median and the two above it. Two groups may fall short, the group of itself (which contributes 2 besides ) and the last, partial group, so the elements greater than number at least
The same count holds for elements smaller than . Whichever side the recursion keeps, it loses at least elements:
Run the same algorithm with groups of 7. Roughly what fraction of the elements is then guaranteed to lie on each side of the pivot?
- , as with groups of 5
Show the answer
C.
Half of the groups have a median , and each of those holds 4 elements (its median and the three above it): . The recursion keeps at most , and is linear, because .
Solving the recurrence
Assume for every (strong induction) and substitute, using :
Rewrite the right-hand side as plus a remainder that must not be positive:
For we have , so makes the remainder at most 0, and :
The principle. Whenever with , level of the recursion tree costs at most , a geometric series. With every level costs about , and there are levels.
| Group size | Guaranteed on each side | Recurrence | Result |
|---|---|---|---|
| 3 | about | ||
| 5 | about | , fractions sum to | |
| 7 | about | , fractions sum to | |
| 9 | about | , fractions sum to |
Count comparisons in the simple version: 6 per group median and for the partition. If and , what is ?
Show the answer
22
gives , so . Blum and colleagues' carefully tuned version, PICK, needs at most about , still well above quickselect's average of for the median. That is why libraries keep median of medians as a fallback rather than the default.
How many comparisons does the median need?
Linear time settles the asymptotics; the constant is another matter, and one of the oldest open questions about comparisons. In the comparison model, for the median of distinct keys:
| Bound | Comparisons | Source |
|---|---|---|
| Upper, deterministic | Blum, Floyd, Pratt, Rivest & Tarjan (1973), algorithm PICK | |
| Upper, deterministic | Schönhage, Paterson & Pippenger (1976) | |
| Upper, deterministic | Dor & Zwick (1999) | |
| Lower, deterministic | Bent & John (1985) | |
| Lower, deterministic | for a tiny | Dor & Zwick (2001) |
| Randomized, expected | , optimal | Floyd & Rivest (1975); matching lower bound by Cunto & Munro (1989) |
Crucial comparisons. Let be the median. A comparison of with is crucial for if it is the first to show or . To certify , the algorithm must know, for each , a chain of comparisons leading from to , and the first link of that chain is crucial for . A comparison is crucial for at most one of its two keys, so
The adversary. It leaves each key unassigned until the key's first comparison, then assigns it to (to end above ) or (to end below ) so that the comparison is crucial for no one: two fresh keys become one and one ; a fresh key compared with an key joins ; one compared with an key joins . Since , an -versus- comparison links neither key to .
Counting. The adversary can do this until or holds keys. Every key other than must take part in some comparison, so one side does fill up, and each wasted comparison adds at most one key to that side. That forces at least wasted comparisons on top of the crucial ones:
Bent and John (1985) pushed the same style of argument to with a subtler accounting of how much each comparison can reveal, and Dor and Zwick (2001) to . The upper bounds came from a different direction: Schönhage, Paterson and Pippenger built the median from carefully structured partial orders ("factories" of small sorted pieces), and Dor and Zwick refined those factories to reach .
In practice, and what is still open
Median of medians is rarely the fastest way to select: its constant is several times quickselect's. Its role is a guarantee. Introselect (Musser, 1997) runs quickselect with cheap pivots and switches to a linear-time pivot rule when the range fails to shrink quickly enough, so typical inputs pay quickselect's price and adversarial ones still finish in . Engineering narrows the gap further: Alexandrescu (2017) designed deterministic selection routines that compete with quickselect in practice.
The history is a small hall of fame: four of the five authors of the 1973 paper, Floyd (1978), Tarjan (1986), Blum (1995) and Rivest (2002), received the Turing Award, each for other work.
Key takeaways
- Median of medians (BFPRT, 1973) groups the keys by 5 and recursively selects the median of the group medians; that pivot has at least keys on each side.
- is linear because ; groups of 3 give fractions summing to 1 and , while groups of 7 or 9 work.
- An adversary that splits fresh pairs across the median forces comparisons; the best known bounds for the median are below and above, a gap open for over two decades.
- In practice median of medians is a fallback (introselect): quickselect is faster on average, and random sampling needs only expected comparisons for the median.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why is the plain algorithm with groups of 3 not linear?
- A group of 3 has no median
- It needs more comparisons per group than groups of 5 do
- The recursion on the medians would never terminate
- It guarantees only about elements on each side, so , whose fractions sum to 1: every level of the recursion costs , and there are levels
Show the answer
D. It guarantees only about elements on each side, so , whose fractions sum to 1: every level of the recursion costs , and there are levels
With groups of 3, half of the groups give 2 elements each above the pivot: . The two recursive calls then cover elements in total, the work no longer shrinks from level to level, and the recurrence solves to .
For distinct keys, at least how many keys does the bound guarantee on each side of the median of medians?
- 300
- 294
- 500
- 100
Show the answer
B. 294
. The pays for two groups that may contribute fewer than 3 elements: the pivot's own group and a final partial group. So each recursive call keeps at most keys.
Which statement about the number of comparisons needed to find the median of distinct keys is true?
- The exact worst-case constant is known to be 2
- Every deterministic algorithm needs at least comparisons in the worst case for some tiny , the best known algorithm needs about , and randomized algorithms need only about on average
- Median of medians is optimal: comparisons are necessary
- Sorting is unavoidable, so comparisons are needed
Show the answer
B. Every deterministic algorithm needs at least comparisons in the worst case for some tiny , the best known algorithm needs about , and randomized algorithms need only about on average
Bent and John (1985) proved , Dor and Zwick raised it to (2001), and their 1999 algorithm uses at most . Floyd and Rivest's sampling algorithm uses comparisons on average for the median, below the deterministic worst-case lower bound.
In the lower-bound argument, what does the adversary answer when the algorithm compares two keys that have never been compared before?
- It answers at random
- It reveals which key is the median
- It declares one key larger, places it above the median and the other below, so the comparison is crucial for neither key
- It places both keys above the median
Show the answer
C. It declares one key larger, places it above the median and the other below, so the comparison is crucial for neither key
Once one key is fixed above the median and the other below it, the comparison links neither of them to the median, so it is wasted for the algorithm. The adversary forces at least such wasted comparisons before one side fills up, on top of the crucial comparisons every algorithm needs.
Further reading
- M. Blum, R. W. Floyd, V. Pratt, R. L. Rivest & R. E. Tarjan, "Time bounds for selection," Journal of Computer and System Sciences 7(4) (1973), 448–461.
- A. Schönhage, M. Paterson & N. Pippenger, "Finding the median," Journal of Computer and System Sciences 13 (1976), 184–199.
- S. W. Bent & J. W. John, "Finding the median requires 2n comparisons," Proceedings of the 17th ACM Symposium on Theory of Computing (1985), 213–216.
- D. Dor & U. Zwick, "Selecting the median," SIAM Journal on Computing 28(5) (1999).
- K. Chen & A. Dumitrescu, "Select with groups of 3 or 4," Algorithms and Data Structures (WADS 2015), LNCS 9214 (2015), 189–199.