Deterministic selection
Median of medians selects the k-th smallest element in worst-case O(n) time. It chooses a pivot that is guaranteed to be reasonably central, so every round discards a constant fraction of the data.
Learning goals
- Run the median-of-medians pivot selection on an example
- Explain why groups of five are used
- Solve the recurrence T(n) = T(n/5) + T(7n/10) + O(n)
- Know when to use it instead of quickselect
Prerequisites
Median-of-medians
The idea is to find a pivot that is guaranteed to be near the middle without already knowing the median. Split the array into groups of five, find the median of each group, then recursively find the median of those medians. Use that as the pivot for ordinary partitioning.
Because the pivot is the median of the group medians, a constant fraction of the elements are provably smaller and a constant fraction provably larger, so quickselect can no longer be stuck with a lopsided split.
1static int select(int[] a, int lo, int hi, int idx) { // idx is a 0-based sorted index2while (true) {3if (hi - lo < 5) { Arrays.sort(a, lo, hi + 1); return a[idx]; }4int groups = (hi - lo + 5) / 5;5for (int g = 0; g < groups; g++) { // median of each group of five6int gl = lo + g * 5, gr = Math.min(gl + 4, hi);7Arrays.sort(a, gl, gr + 1);8swap(a, lo + g, gl + (gr - gl) / 2); // collect medians at the front9}10int pivot = select(a, lo, lo + groups - 1, lo + (groups - 1) / 2); // median of medians11int p = partitionAroundValue(a, lo, hi, pivot);12if (idx == p) return a[p];13if (idx < p) hi = p - 1; else lo = p + 1;14}15}
Grouping of five
Worked example with 15 values: [9, 4, 7, 1, 8 | 15, 12, 6, 20, 3 | 11, 2, 18, 5, 14]. Sort each group of five and take its middle value. The medians are 7, 12 and 11, and the median of those three is 11, which becomes the pivot.
Partitioning around 11 puts nine values below it (9, 4, 7, 1, 8, 6, 3, 2, 5), the pivot itself, and five above (15, 12, 20, 18, 14). The pivot has rank 10 of 15, fairly central, as promised.
| Group | Values | Sorted | Median |
|---|---|---|---|
| 1 | 9, 4, 7, 1, 8 | 1, 4, 7, 8, 9 | 7 |
| 2 | 15, 12, 6, 20, 3 | 3, 6, 12, 15, 20 | 12 |
| 3 | 11, 2, 18, 5, 14 | 2, 5, 11, 14, 18 | 11 |
| Medians | 7, 12, 11 | 7, 11, 12 | 11, used as the pivot |
Linear worst-case selection
Finding the group medians costs O(n): sorting a group of five takes a constant number of steps. The recursive call on the n/5 medians costs T(n/5). Partitioning costs O(n). The recursive call on the remaining side costs at most T(7n/10), as shown in the next section.
T(n) ≤ T(n/5) + T(7n/10) + c·n. Since 1/5 + 7/10 = 9/10 < 1, the total work shrinks by a constant factor each level and the solution is T(n) = O(n). Using groups of three gives 1/3 + 2/3 = 1, which only solves to O(n log n).
Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(log n) stack
Correctness intuition
Why is the pivot central? Half of the ⌈n/5⌉ group medians are less than or equal to the pivot. Each such median brings the two smaller members of its group along, so at least 3 · ⌈n/10⌉ ≈ 3n/10 elements are less than or equal to the pivot. By symmetry, at least 3n/10 are greater than or equal to it.
So after partitioning, each side has at most 7n/10 elements, plus a small constant for rounding. This is the bound the recurrence uses. Correctness itself is ordinary quickselect: whichever pivot is chosen, the answer lies on the side containing the target index.
Practice ladder
Now try it yourself
Easy
- Compute the median of medians for [3, 8, 1, 9, 4, 7, 2, 6, 5, 10] by hand
- Why must the array be split into groups of an odd size?
Medium
- Implement the algorithm and verify it against sorting on random inputs
- Show that groups of 7 also give O(n)
Advanced
- Prove the 3n/10 bound including rounding
- Use median-of-medians as the pivot rule in a worst-case O(n log n) quick sort
Key takeaways
- Median of medians guarantees a pivot with at least 3n/10 elements on each side.
- T(n) = T(n/5) + T(7n/10) + O(n) = O(n) because 1/5 + 7/10 < 1.
- It is a theoretical and fallback tool; randomised quickselect is faster in practice.