Linear time selection — median of medians algorithm
WHAT are we even solving?
WHY not just sort? Sorting gives all order statistics but costs . We only need one. Doing more work than necessary is the thing 80/20 tells us to avoid — so we aim for .
HOW Quickselect works (the foundation)
Quickselect = Quicksort that only recurses into the side containing .
- Pick a pivot .
- Partition around : smaller elements left, larger right. Pivot lands at its final sorted index .
- If → done. If → recurse left. If → recurse right (adjust ).
The trick: Median of Medians chooses the pivot
WHY 5? It's the smallest odd group size for which the recursion math closes (gives linear time). We'll see exactly where 5 comes from in the derivation. (Groups of 3 fail; 7 also works but 5 is optimal in practice.)

WHY this pivot is provably good (the heart)
Let be the median of the group-medians. We bound how many elements are guaranteed smaller than (and by symmetry, larger).
By symmetry, at least elements are . Therefore the larger side of the partition has at most elements.
Worked example 1 — finding the pivot
Array (n=15): [12, 3, 5, 7, 19, 1, 8, 22, 4, 6, 15, 9, 2, 11, 17], want (the median).
Step 1 — groups of 5.
[12,3,5,7,19] [1,8,22,4,6] [15,9,2,11,17]
Why? Constant-size groups make per-group median trivial.
Step 2 — medians of each group (sort the 5, take middle):
[3,5,7,12,19]→ 7[1,4,6,8,22]→ 6[2,9,11,15,17]→ 11
Step 3 — median of medians of [7,6,11] → 7. So pivot .
Why this matters: 7 is guaranteed not to be near an extreme, so partitioning around it removes a constant fraction.
Step 4 — partition around 7: elements : [3,5,1,4,6,2] (6 of them) → 7 lands at index 7 (1-indexed). Since , recurse right into [12,19,8,22,15,9,11,17] for the st smallest = 8. ✅ (8 is indeed the median of the original.)
Worked example 2 — why a bad pivot would hurt (steel-man)
Same array, but suppose we pick pivot = 1 (the min).
Partition: nothing on the left, 1 lands at index 1. We must recurse into the remaining 14 elements. We removed only one element for work.
Why this step shows the danger: if every pivot were a min/max, we'd do . MoM forbids this because always has elements on each side.
Common mistakes (Steel-man them)
Forecast-then-Verify
Pseudocode (self-contained)
Select(A, k):
if len(A) <= 5:
return sorted(A)[k-1]
medians = [median(group) for group in chunks(A, 5)]
M = Select(medians, ceil(len(medians)/2)) # median of medians
L = [x for x in A if x < M]
E = [x for x in A if x == M]
G = [x for x in A if x > M]
if k <= len(L): return Select(L, k)
elif k <= len(L)+len(E): return M
else: return Select(G, k - len(L) - len(E))
Why the 3-way split (L, E, G)? It safely handles duplicates of so we never recurse on a side that can't shrink.
Flashcards
What problem does selection solve?
Why can plain Quickselect be ?
What is the median-of-medians pivot rule?
How many elements are guaranteed (the MoM pivot)?
What is the MoM recurrence?
Why does that recurrence give ?
Why does group size 3 fail?
Why use MoM over random pivots in practice?
Why the 3-way (L,E,G) partition?
Which fraction inequality is the crux of linearity?
Recall Feynman: explain to a 12-year-old
Imagine 15 kids and you want the kid of middle height — but lining everyone up takes forever. Trick: split kids into little teams of 5. In each team, find the middle-height kid. Now look only at those middle kids and find their middle kid. That kid is a "pretty middle" kid — never the shortest or tallest. Stand everyone shorter on the left, taller on the right. Whichever side has the kid you want, repeat the game there. Because each round throws away a big chunk of kids, you finish super fast — and the trick guarantees you never get unlucky.
Connections
- Quickselect — MoM is the pivot-choosing upgrade that makes it worst-case linear.
- Quicksort — same partition machinery; MoM pivot gives worst-case quicksort.
- Order statistics — median, min, max are special cases of selection.
- Master Theorem / Recurrence relations — but note MoM needs substitution, not the Master Theorem (unequal subproblems).
- Randomized algorithms — contrast: expected vs worst-case guarantees.
- Partitioning (Lomuto vs Hoare) — the engine behind step 4.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, problem simple hai: ek unsorted array me -th sabse chhota element nikalna hai, lekin pura array sort nahi karna (kyunki sort leta hai, aur humein sirf ek element chahiye). Quickselect isko average me me kar deta hai, par agar pivot har baar bekaar (min ya max) chun liya, toh worst case ho jata hai. Yahi galti se bachne ke liye Median of Medians trick aata hai.
Trick ye hai: array ko 5-5 ke groups me baanto, har group ka median nikaalo (5 elements ka median nikalna constant kaam hai), phir un saare medians ka median recursively nikaalo — usko pivot bana lo. Magic ye hai ki kabhi bhi bahut chhota ya bahut bada nahi ho sakta: kam se kam elements se chhote aur bade guaranteed hote hain. Matlab partition ke baad bachi hui side se zyada nahi hoti.
Isi guarantee se recurrence banta hai . Yahan sabse important baat: , jo se kam hai — isi wajah se recursion linear me collapse ho jaata hai aur answer aata hai. Agar group size 3 lo toh fractions ban jaate hain, aur linear nahi rehta — isiliye 5 use karte hain.
Exam/interview ke liye yaad rakho: MoM ka asli faayda worst-case guarantee hai, practical speed nahi (constant factor bada hota hai). Real code me log randomized quickselect ya introselect use karte hain, par theory aur adversarial inputs ke liye MoM gold standard hai. "9/10 < 1" wali line hi pura concept hai — wahi 80/20 hai.