Median of Medians
Context: FIT2004_MOC · the deterministic pivot rule that converts Quickselect’s expected into worst case — and Quick Sort’s worst case into (scope: lecturer-flagged as not examinable in the final exam historically, but live in the weekly quiz — learn the core concepts and the recurrence, do not drill it like a [P] hand skill)
Quick Revision
- 🎯 Objective: spend choosing a pivot that is provably near the middle ➔ every partition splits at worst , so the recursion can never degenerate.
- ⚡ Key Constraint: it is a guarantee, not a speed-up — the constant factor is large enough that a random pivot beats it in practice; reach for it only when a worst-case bound is the requirement.
📝 Core
- Procedure ➔ split into groups of ➔ sort each group by insertion sort and take its median ( per group, items) ➔ recursively Quickselect the median of those medians ➔ use it as the partition pivot.
- The split guarantee ➔ at least half the group medians are the median-of-medians , and each such group contributes elements its own median ⟹ elements sit on each side ⟹ neither side exceeds .
- The recurrence and why it closes ➔ pivot-finding costs , the surviving side costs , partitioning costs :
- Sub-unit shrinkage is the whole proof ➔ the two subproblems consume only of the input, so the per-level work forms a decaying Geometric Series with , summing to — root-dominated, hence linear.
- Why groups of ➔ groups of give with ⟹ all levels equal ⟹ ; the fractions must sum to strictly less than , and is the smallest odd group size that achieves it.
- Where it plugs in ➔ as Quickselect’s pivot ⟹ worst-case selection; as Quick Sort’s pivot ⟹ worst-case sorting, i.e. the answer to “how do you ensure the worst case never occurs?”
⚠️ Common Mistakes
- 💡 Forgetting the recursive call ➔ finding the median of the medians is itself a selection problem; scanning or sorting them instead costs and destroys the linear bound.
- 💡 Selling it as “faster quickselect” ➔ it is strictly slower on typical input; the deliverable is the removal of the tail, nothing else.
- 💡 Claiming the pivot is the true median ➔ it is only guaranteed to lie between the th and th percentile — a constant-fraction split, which is all the recurrence needs.
🧠 Active Recall
Why must the two recursive fractions sum to strictly less than , and what breaks at exactly ?
- Hint: Write the per-level work as a series and read the ratio.
Answer
- Short answer: makes the level work decay ⟹ root-dominated ; at exactly every level costs ⟹ .
- Why: The ratio is the regime ➔ total work is with the surviving fraction; sums to the constant , sums to the number of levels ➔ Solving Recurrences (Telescoping). Groups of land exactly on , which is why is the textbook choice.
A random pivot already gives expected time. What does Median of Medians actually buy?
- Hint: Name what an expectation does not rule out.
Answer
- Short answer: It converts a claim about the average over pivot choices into a claim about every run on every input.
- Why: Expected bounded ➔ randomisation stops an adversary from constructing a bad input, but an unlucky sequence of pivots is still possible; a deterministic guarantee removes the bad case from the algorithm rather than from the input distribution ➔ Algorithmic Complexity.