Definition · Median of mediansTo choose a pivot of a[1..n] by median of medians with group size g: split the array into ⌈n/g⌉ groups of g consecutive elements, find each group's median by direct sorting, and return the median of those ⌈n/g⌉ medians, found by applying the whole selection algorithm to them recursively.
- Split into groups of g; sort each group and take its median. Cost Θ(n), since g is a constant.
- Recursively select the median of the ⌈n/g⌉ medians. This is the first recursive term.
- Partition the array around that pivot.
- If the pivot's rank is the one wanted, return it; otherwise recurse into the surviving side. This is the second recursive term.
Theorem · The discard guaranteeWith groups of five, the median of medians p has at least 3(⌈⌈n/5⌉/2⌉ − 2) elements at most p and at least as many at least p. Asymptotically that is 3n/10, so at most 7n/10 elements survive the partition.
Half of the ⌈n/5⌉ group medians are at most p, by the definition of a median. Take any such group: its own median is at most p, and so are the two elements of that group below its median. That is three elements at most p from each of those groups.
Two groups have to be set aside: the group containing p itself, and the last group if it is not full. That is where the − 2 comes from. Everything else is symmetric for the elements at least p.
So at least about 3n/10 elements are on each side of the pivot, and the side that survives the partition holds at most n − 3n/10 = 7n/10 of them.
□
T(n) ≤ T(n/5) + T(7n/10) + cn
level 0 cn
level 1 c(1/5 + 7/10)n = c(9/10)n
level 2 c(9/10)²n
…
total cn · 1/(1 − 9/10) = 10cn
groups of 3: 1/3 + 2/3 = 1 every level costs cn, and there are log n
So the whole argument is one comparison of a sum with one, and the lab does it as a comparison of integers: 1/5 + 7/10 = 9/10 and 1/3 + 2/3 = 1 are computed as exact rationals, because 0.8999999999999999 is not an argument. Groups of seven give 1/7 + 5/7 = 6/7, which also converges, and at n = 45 the level sums total 315 = 7n.
Example · One round at forty-five elementsThe lab's array at n = 45 is a seeded shuffle of 1 … 45. Its nine group medians are 24 35 21 27 14 38 8 16 25, and the median of those is 24. Partitioning around 24 leaves 23 elements below and 21 above. The guarantee for this size and group is 9, so the round discarded 21 where it had promised 9 — and the promise is the part that holds for every array of 45 elements.
That gap between 9 and 21 is the slack a proof about every input gives away, and the lab prints both numbers on every round rather than only the one that flatters the algorithm. Note also that the pivot 24 is not the median of 1 … 45, which is 23. It never had to be.
Why five, and what three costs
The group size g fixes both fractions: the median recursion is on n/g elements, and the guaranteed discard is (g+1)/(4g), so the survivor fraction is 1 − (g+1)/(4g). At g = 3 those are 1/3 and 2/3, summing to exactly one, and a recursion tree whose levels do not shrink costs n at each of about log n levels. At g = 5 they sum to 9/10 and the tree is geometric. Three is not slightly worse; it is a different complexity class.
Larger groups keep the sum below one — seven gives 6/7 — while raising the constant cost of sorting each group, so five is the smallest odd size that works and is the one everybody uses. The lab lets you set all three and prints the level sums for each, so the claim is checkable rather than received.