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