Learn
Lesson · Sorting and Selection · Selection, bounds and scale

Median of Medians

Break the array into groups of five, take each group's median, and take the median of those medians as the pivot — recursively. The pivot is then guaranteed to lie in the middle forty per cent, so at least 3n/10 elements are discarded and the recurrence is T(n) ≤ T(n/5) + T(7n/10) + cn. That is linear because 1/5 + 7/10 is less than one, and with groups of three the same two fractions sum to exactly one and it is not.

groups of 5, median of each, pivot = median of those medians (recursively) guaranteed: at least 3n/10 discarded, so at most 7n/10 survives T(n) ≤ T(n/5) + T(7n/10) + cn 1/5 + 7/10 = 9/10 < 1 ⟹ linear n = 45, groups of 5: the level sums total exactly 450 = 10n groups of 3: 1/3 + 2/3 = 1 every level costs n, so n log n
The pivot rule, the guarantee, and the sum that decides linearity

Core ideas

What this lesson establishes

The hard idea is that the two fractions of a two-term recurrence decide everything, and that they are decided in turn by the group size.

1

The pivot only has to be near the middle

Let p be the median of the ⌈n/5⌉ group medians. At least half of those medians are at most p, and each of their groups contributes three elements at most p — its median and the two below it. So at least 3⌈⌈n/5⌉/2⌉ elements are at most p, which is about 3n/10, and symmetrically about 3n/10 are at least p. The pivot is not the median and does not need to be: it is guaranteed to sit in the middle forty per cent.

2

Linearity is decided by whether the fractions sum to under one

The recurrence has two recursive terms — one call on the n/5 medians to find the pivot, one on the at most 7n/10 survivors — so the master theorem does not apply. The recursion tree does: level zero costs cn, level one costs c(1/5 + 7/10)n, level two c(9/10)²n, and the sum is geometric with ratio 9/10, giving 10cn. The lab evaluates those level sums as exact fractions, and at n = 45 they total exactly 450 = 10n.

3

The guarantee costs a constant, and the constant is visible

On the lab's 45-element array, median of medians spends 299 comparisons and quickselect spends 131 for the same rank. That is the price of removing the word “expected”: quickselect is linear in expectation with a small constant, median of medians is linear on every input with a large one. In practice the two are combined, and the theoretical value of this one is that it makes the deterministic bound available at all.

The material

A pivot with a guarantee, and the recurrence it buys

The counting argument for the discard, the level sums that solve a two-term recurrence, and what group size three does to them.

Definition · Median of medians

To 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.

  1. Split into groups of g; sort each group and take its median. Cost Θ(n), since g is a constant.
  2. Recursively select the median of the ⌈n/g⌉ medians. This is the first recursive term.
  3. Partition the array around that pivot.
  4. 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 guarantee

With 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 elements

The 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.

Interactive

The pivot only has to be near the middle

Groups of three, five and seven, and the sum that decides which of them is linear

A pivot with a guarantee, and the recurrence it buysthe level sums of T(n) = T(f1 n) + T(f2 n) + n, as exact fractions
work at this levelthe sum convergesthe sum does notthe guaranteed discard

Method

Working one round, then solving the recurrence

The pivot selection is mechanical. The part worth slowing down for is the level sums.

1

Group, sort each group, and collect the medians

Groups of five, in order, and sort each one by hand — five elements is a handful of comparisons and the constant is why the group size cannot grow much. Write the medians out as their own list.

2

Select the median of that list, recursively

This is the same problem on a fifth of the data, which is the first recursive term. On 45 elements the list of medians has nine entries and its median is the pivot.

3

Partition, and compare the actual discard with the guarantee

Count how many elements fell on each side. The lab prints the guarantee for that round beside the smaller side; the actual figure is normally far larger, and that difference is the price of a claim that covers every array rather than this one.

4

Write the recurrence and sum its levels

T(n) ≤ T(n/g) + T(1 − (g+1)/(4g))n + cn. Add the two fractions. If the sum is below one, the level sums are geometric and total n/(1 − sum); if it is one, every level costs n and there are about log n of them.

5

Compare the measured cost with quickselect's on the same array

299 against 131 at 45 elements. Both are one input's cost. The claim that distinguishes the two algorithms is not in those numbers: it is that one of them holds for every array and the other holds in expectation.

Guarantees, and the sum that decides

Score 0 / 0

Forty-five elements, groups of five, one round by hand

The array is the lab's seeded shuffle of 1 … 45, taken in order, nine groups of five. Each group is sorted and its median taken; the median of the nine medians is the pivot.

group as they appear sorted median 1 31 40 22 24 15 15 22 24 31 40 24 2 13 23 35 43 42 13 23 35 42 43 35 3 21 20 10 41 34 10 20 21 34 41 21 4 19 37 3 30 27 3 19 27 30 37 27 5 1 14 2 17 18 1 2 14 17 18 14 6 39 11 28 38 44 11 28 38 39 44 38 7 7 32 8 4 33 4 7 8 32 33 8 8 6 29 5 16 45 5 6 16 29 45 16 9 9 26 25 12 36 9 12 25 26 36 25 medians 24 35 21 27 14 38 8 16 25 sorted 8 14 16 21 24 25 27 35 38 median of them 24 ← the pivot partition around 24: 23 elements below, 21 above guaranteed for n = 45, g = 5: 9 on the thin side the true median of 1 … 45 is 23, so the pivot is NOT the median 1/5 + 7/10 = 9/10 < 1 level sums at n = 45: 45, 40.5, 36.45, … total exactly 450 = 10n

Check the guarantee against the round. Five of the nine medians are at most 24, and each of their groups contributes three elements at most 24; setting aside the group holding the pivot leaves four groups, so twelve elements are guaranteed below, and the lab's per-round figure of 9 is the same count with the partial-group allowance made. The round actually delivered 23. The guarantee is the number that survives being asked about every array.

The level sums are the other half. 45, 40.5, 36.45, … is a geometric sequence with ratio 9/10, and 45/(1 − 9/10) = 450. Switch the group size to three in the lab and the level sums become 45, 45, 45, … — the column stops shrinking, and there is no total to print.

For a faded rehearsal, set the group size to seven and predict two things before reading them: the two fractions, and whether the tree converges. The supplied first move is the discard formula (g+1)/(4g), which at g = 7 is 8/28 = 2/7. Work out the survivor fraction and the sum, then say why seven is not used in practice even though its sum is smaller than five's.

Common mistakes

1
Believing the pivot is the true medianIt is the median of the group medians, which is a different element: 24 where the true median is 23 in the worked round. The algorithm does not need it to be the median — it needs a guaranteed fraction on each side, and that is all the proof uses.
2
Reaching for the master theoremT(n) ≤ T(n/5) + T(7n/10) + cn has two recursive terms with different fractions and no af(n/b) shape, so the master theorem does not apply to it. What solves it is the recursion tree: sum each level, notice the ratio, and sum the geometric series. Discrete Mathematics' “Recursion Trees and Amortised Analysis” is where that technique was proved.
3
Assuming a smaller group size is a small changeGroups of three make the two fractions sum to exactly one, and the recursion tree stops shrinking: n per level, about log n levels, Θ(n log n) in total. That is the same class as sorting, so the entire point of the algorithm is lost. The lab prints the level sums for group sizes three, five and seven side by side.

Check your understanding

✓
Finish when you can defend the group size with the two fractions rather than with a convention.

You should be able to run one round of the pivot selection on paper, state the discard guarantee and where the − 2 comes from, solve the two-term recurrence by its level sums, and say what changes at group size three and why it is a change of class.

Note: Two lessons have now beaten sorting by asking for less. The last two ask what cannot be beaten at all. “Adversary Arguments” proves lower bounds for the maximum, for both ends at once and for the second largest, and every one of them is met exactly by an algorithm you can run.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.