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: the deck’s old “not examinable” line is struck through and replaced with examinable from 2023 onwards — the lecturer points at his hand-execution video and the Sanity Check. Treat it as a hand skill: be able to run it on paper and state the recurrence.

Quick Revision

  • 🎯 Objective: spend choosing a pivot that is provably near the middle ➔ every partition splits at worst , so the recursion can never degenerate.
  • 📦 Core Components: groups of ➔ medians | recursive select ➔ the median of those medians | partition ➔ on that pivot.
  • ⚡ 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.
  • Base case ➔ ⟹ insertion-sort the whole thing and return its median directly; this is what terminates the mutual recursion.
  • Co-recursion ➔ median_of_medians calls quickselect, and quickselect calls median_of_medians for its pivot — the lecturer’s named term. Both shrink their input every round, so the pair terminates at the base.
  • The split guarantee ➔ the median-of-medians exceeds half the group medians, and each such group contributes of its elements its own median ⟹ of the input is ; symmetrically is ⟹ sits in the middle , and neither side of the partition exceeds .
  • The recurrence and why it closes ➔ pivot-finding costs , the surviving side costs , partitioning plus the group medians cost :
  • 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 ➔ Solving Recurrences (Telescoping).
  • 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?”

⚙️ Core Implementation

🔹 The co-recursive pair

📊 Exam Execution Trace

Manual Execution Trace

Find the MoM pivot of 12, 3, 45, 7, 19, 22, 8, 31, 1, 27, 14, 40, 5, 25, 33 ( ⟹ groups).

GroupItemsSorted (insertion, items)Median
12, 3, 45, 7, 193, 7, 12, 19, 45
22, 8, 31, 1, 271, 8, 22, 27, 31
14, 40, 5, 25, 335, 14, 25, 33, 40
selectmedians 12, 22, 2512, 22, 25

Check the guarantee: partitioning on gives elements below and above — a split, comfortably inside the promised band, and both sides clear the floor. Note the groups are not left sorted in the array; they are sorted only long enough to read a median.

⚠️ 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 converting an expected bound into a worst-case one (Algorithmic Complexity) by removing the tail, nothing else.
  • 💡 Claiming the pivot is the true median ➔ it is only guaranteed to lie in the middle — a constant-fraction split, which is all the recurrence needs.

🧠 Active Recall