3.6.10 · D3Sorting & Searching

Worked examples — Linear time selection — median of medians algorithm

2,444 words11 min readBack to topic

This page drills the median-of-medians algorithm until no input can surprise you. We march through every case class the algorithm meets — the ordinary median, the exact-pivot hit, the extreme minimum, arrays clogged with duplicates, arrays too small to group, and the exam-style "prove the bound" twist.

Nothing here assumes you already trust the algorithm. Each example rebuilds the moving parts: the groups of 5, the group medians, the median of those medians (our pivot ), and the 3-way partition into (less), (equal), (greater).


The scenario matrix

Every worked example below is tagged with the matrix cell it exercises. Together the eight examples touch every cell.

Cell Scenario class What could break Covered by
C1 exactly equals → pivot is the answer, return mis-routing a that should stop now Ex 1
C2 small → recurse left into forgetting to keep unchanged Ex 2
C3 large → recurse right into forgetting to subtract Ex 3
C4 Even number of medians + pivot near extreme undefined median tie-break; guarantee off-by-one Ex 4
C5 Many duplicates equal to recursing on a side that can't shrink Ex 5
C6 Degenerate base case: grouping loop never runs Ex 6
C7 not a multiple of 5 (leftover group) odd-sized last group median Ex 7
C8 Word problem + exam-twist bound mis-stating the guarantee Ex 8

Example 1 — lands exactly on the pivot (cell C1)

Forecast: After we pick pivot , will land in , , or ? Guess the pivot's final sorted index — and whether we recurse at all.

  1. Group into fives. [12,3,5,7,19] [1,8,22,4,6] [15,9,2,11,17]. Why this step? Constant-size groups make each group median — the whole speed argument rests on this.
  2. Median of each group (sort 5, take position ). [3,5,7,12,19]→7, [1,4,6,8,22]→6, [2,9,11,15,17]→11. Why this step? These three medians are "typical" representatives of their groups.
  3. Median of medians. Of [7,6,11], sorted [6,7,11], position . Why this step? is our provably-not-extreme pivot (see Order statistics).
  4. 3-way partition around 7. (), , . Why this step? The pivot's sorted position is .
  5. Route . Is ? No. Is ? Yes → the answer is exactly . We recurse nowhere. This is the " hits the pivot" case.

The figure below sorts and colors the three buckets, so you can see that (red arrow) points straight at the orange pivot bar — the algorithm halts the instant enters .

Figure — Linear time selection — median of medians algorithm

Verify: Sort [1,2,3,4,5,6,7,8,9,11,12,15,17,19,22]; the 7th value is , equal to . ✅


Example 2 — small, recurse left (cell C2)

Forecast: Does stay inside ? Does the target index change when we recurse?

  1. Reuse the pivot , , . Why this step? The pivot depends on , not on — so it's identical to Ex 1.
  2. Route . , so recurse left into with target unchanged: . Why this step? Everything in is smaller than everything in , so ranks inside match global ranks. No subtraction.
  3. Solve inside . Sorted: [1,2,3,4,5,6]; the 3rd is .

Verify: Global sorted array's 3rd element is . ✅ (And note we did not subtract anything — the classic C2 trap.)


Example 3 — large, recurse right (cell C3)

Forecast: After routing right, what is the adjusted target? Guess before computing.

  1. Pivot , , .
  2. Route. , recurse into with . Why this step? We already skipped past the 7 elements ; the 13th overall is the 6th of what's left.
  3. Solve inside . Sorted : [8,9,11,12,15,17,19,22]; the 6th is .

Verify: Global sorted 13th element is . ✅


Example 4 — even number of medians, pivot near an extreme (cell C4)

Forecast: How much does pivot throw away? With only 2 group-medians, which one does our convention pick? And is the "" bound exact for , or does a floor sneak in?

  1. Bad pivot . , , (9 elements). We removed one element for work. Why this step? If every pivot were the min, cost — exactly what Quickselect risks in the worst case.
  2. Honest MoM pivot — apply the median convention. Groups [1,2,3,4,5]→3 and [6,7,8,9,10]→8, so the medians list is [3,8] with items. Our rule picks position → the lower middle → . Why this step? Two medians is exactly the ambiguous even case; the convention (fixed above) makes deterministic rather than a coin flip.
  3. Guarantee with the honest floor. The clean "" formula is an asymptotic bound; for a specific small we must floor the counts. Here half the medians is 1 group whose median and the two below it are , giving candidates — but that count includes and elements outside strict . Concretely so , : the elements number , matching . The strict-less set can be smaller — the guarantee counts , not . Why this step? The reviewer-style off-by-one comes from confusing "" (which is ) with "strictly " (which is ). The theorem bounds .
  4. Surviving side. has elements, and . Exactly on the bound.

The figure contrasts the two pivots side by side: the orange bar is the pivot, and you can see the honest MoM pivot () leaves at most 7 green survivors, whereas leaves 9.

Figure — Linear time selection — median of medians algorithm

Verify: number of elements is ; strict ; survivors ; and the even-median rule gives . ✅


Example 5 — a swamp of duplicates (cell C5)

Forecast: If and most of the array equals 5, does the 2-way " / " split loop forever? Why does the 3-way split save us?

  1. Groups of 5. [5,5,5,5,5]→5, [5,5,2,9,5] sorted [2,5,5,5,9]→5. Medians [5,5], , position .
  2. 3-way partition. (), (), (). Why this step? A naive "less-than / greater-or-equal" split would dump all eight 5's on one side and recurse on a 9-element subproblem that never shrinks — an infinite/quadratic trap. The bucket absorbs the ties.
  3. Route . Is ? No. Is ? Yes → the answer is .

Verify: Sort [2,5,5,5,5,5,5,5,5,9]; 8th element . ✅


Example 6 — degenerate base case (cell C6)

Forecast: Does the grouping loop run at all here?

  1. Base-case check. Since , we do not group. We sort directly and index. Why this step? Grouping into fives with one group is pointless; sorting 5 items is constant work. This is the recursion's floor.
  2. Sort and index. Sorted: [3,7,7,42,99]; the 4th (1-indexed) is .

Verify: sorted([42,7,7,99,3])[4-1] == 42


Example 7 — leftover group when (cell C7)

Forecast: How big is the last group, and what is the median of a 3-element group?

  1. Group into fives, last group short. [20,4,17,9,1] [14,6,11,2,8] [19,15,7]. The last group has only 3 elements — that's fine. Why this step? Real arrays rarely divide evenly; the median of a short group is just its middle after sorting.
  2. Group medians (position ). [1,4,9,17,20]→9, [2,6,8,11,14]→8, [7,15,19]→15. Medians [9,8,15].
  3. Median of medians. Of [9,8,15], sorted [8,9,15], position .
  4. Partition around 9. (), , .
  5. Route . → recurse left into , target unchanged . Sorted : [1,2,4,6,7,8]; 6th is .

Verify: Sort [1,2,4,6,7,8,9,11,14,15,17,19,20]; the 6th element is . ✅


Example 8 — word problem + exam-twist bound (cell C8)

Forecast: Which is the 95th percentile? What fraction does one level shave off?

  1. Translate percentile to . The 95th percentile is the -th order statistic with . Why this step? Order statistics turns "percentile" into "which sorted index" — a plain selection query.
  2. Why MoM, not random? The requirement says hard worst-case. Randomized Quickselect is only in expectation; an adversarial or unlucky ordering gives . MoM's guarantee is unconditional.
  3. Bound the surviving side. At least elements are and at least are , so each side is at most . For : . Why this step? This is the inequality that caps how much can survive to the next level.
  4. Total work. By the recurrence and , so — the hard budget is honored.

The recursion-tree figure plots the work at each level as a fraction of ; each bar is of the previous, so the bars form a shrinking geometric series that sums to — visibly linear.

Figure — Linear time selection — median of medians algorithm

Verify: ✅ and ✅ and


Recall

Recall When does the algorithm return

without recursing? When — i.e. lands in the bucket (cells C1 and C5).

Recall Which cells recurse with an

unchanged ? Left recursion into (cells C2, C7) keeps the same. Right recursion into (C3) subtracts .

Recall How do we take the median of an even-length list?

Position — the lower of the two middles. This resolves the 2-median case in Ex 4/Ex 5.

Recall Why does the 3-way split matter for duplicates (C5)?

The bucket absorbs all copies of , so if lands there we return immediately and never recurse on a side that can't shrink.

Recall What guarantees the surviving side

? At least elements (counting those , floored for finite ) sit on each side of , so no side exceeds .


Flashcards

When you recurse LEFT into , what happens to ?
It stays unchanged, because ranks inside equal global ranks.
When you recurse RIGHT into , what is the new target?
.
When does Select return without recursing?
When (k lands in the equal bucket).
How do you take the median of an even number of items here?
Position — the lower of the two middles.
Why is a 3-way split needed for duplicates?
The bucket absorbs ties to , so we return if lands there and never recurse on a non-shrinking side.
What is the base case of Select?
When , sort directly and return the -th element.
For , what is the largest surviving subproblem after one MoM level?
.
How do you convert a 95th percentile into a selection query?
Find the -th order statistic with .