Short answer: Median of medians chooses a quickselect pivot by splitting the array into groups of five, taking each group’s median, and recursively selecting the median of those medians. That pivot is guaranteed to drop a constant fraction of the array. The recurrence T(n) ≤ T(n/5) + T(7n/10) + O(n) solves to O(n) in the worst case. Groups of three do not give this fraction, and the same recurrence shape then stays superlinear.
What the problem actually asks
Randomized quickselect is expected linear and worst-case quadratic. A deterministic linear selection needs a pivot whose rank sits between two constant fractions of n, so that whichever side rank k falls on, the recursive call is at most cn for some c < 1. The median of every group of five is a witness that is cheap to compute and good enough to guarantee that fraction.
The group size is the lever. Five is the smallest odd constant for which the algebra closes below n. Three looks simpler and does not close.
What the algorithm guarantees
About n/5 group medians are computed in linear time, because each group of five is a constant-size sort. The recursive call that finds the median of those medians costs T(n/5). At least roughly 3n/10 items are strictly on each side of that pivot, up to small floors and the handling of equals, so the larger remaining side has size at most about 7n/10. The master recurrence T(n) ≤ T(n/5)+T(7n/10)+O(n) is O(n), since 1/5+7/10 = 0.9 < 1.
Groups of three produce a pivot that may leave about 2n/3 of the items on the larger side, and T(n) ≤ T(n/3)+T(2n/3)+O(n) solves to O(n log n). The algorithm would still be correct and would no longer be linear. The practical constant for groups of five is large enough that random quickselect is faster almost everywhere. Median of medians is the worst-case proof.
The working steps
- If n is below a small cutoff, sort the slice and return index k.
- Group the slice into fives. Sort each group and collect the medians. A leftover group of fewer than five items is sorted the same way.
- Recursively select the median of that list of medians. Call it the pivot.
- Partition the original slice so the pivot ends at its true rank p.
- If p = k, return it. If k < p, recurse on the left side. Otherwise recurse on the right side. The recursive slice is at most about 7n/10.
The linear bound is a worst-case comparison bound for one order statistic. It does not sort the array. It is also not the method to use merely because it is linear: the constant hidden by O(n) is large, and a random pivot has a smaller expected constant with a quadratic case that a depth cap or a random shuffle already makes irrelevant for many callers.
Approaches that look simpler and fail
| Approach | Why it fails when the group size does not guarantee a fraction |
|---|---|
| Groups of three | The larger side can hold about 2n/3 of the items. T(n) ≤ T(n/3)+T(2n/3)+O(n) is O(n log n), so the linear claim is false. |
| The median of the group medians, computed by sorting those medians | Sorting n/5 medians costs (n/5) log(n/5), and the recurrence is no longer the linear one. The median of medians is itself selected by this algorithm. |
| A guarantee quoted as exactly 30 percent for every n | Floors, the leftover group, and equal keys change the count by O(1) per group. The proof uses at least 3n/10 − O(1), which is enough for a linear solution and is not a promise of 0.3n at n = 5. |
| Use it as the library select because the worst case is linear | The leading constant exceeds a random quickselect’s expected cost by a wide margin. The deterministic pivot is for a hard deadline, not for the average array. |
Expected linear selection belongs to quickselect with a random pivot. A full sort belongs to mergesort or heapsort. Median of medians is the pivot strategy that upgrades one selection to a worst-case linear bound.
A working example
This Python fragment selects with groups of five. The median of medians is the pivot, so the rank is found in linear time. Rank 4 of this array is 5.
def select(a, k):
a = a[:]
if len(a) <= 5:
return sorted(a)[k]
def partition(pivot):
lows = [x for x in a if x < pivot]
highs = [x for x in a if x > pivot]
eqs = [x for x in a if x == pivot]
return lows, eqs, highs
medians = []
for i in range(0, len(a), 5):
group = sorted(a[i:i + 5])
medians.append(group[len(group) // 2])
pivot = select(medians, len(medians) // 2)
lows, eqs, highs = partition(pivot)
if k < len(lows):
return select(lows, k)
if k < len(lows) + len(eqs):
return pivot
return select(highs, k - len(lows) - len(eqs))
data = [9, 1, 7, 3, 8, 2, 5, 4, 6]
assert select(data, 4) == sorted(data)[4] == 5
print(select(data, 4))
Where it is used
Median of medians is the worst-case linear selection algorithm. Library order statistics, such as C++ nth_element, use introselect, which is quickselect with a fallback. The median-of-medians pivot is the existence proof, not the code that runs.
When you can drop this algorithm
Drop median of medians in ordinary code that can draw a random pivot. The expected linear algorithm is shorter and faster. Keep groups of five when selection must be O(n) on every input, including an input built by an adversary who can see a deterministic pivot rule.
Why this stays published
We publish this so a search for median of medians returns the groups-of-five recurrence and the reason groups of three are not linear. Median of medians selects the k-th item in worst-case linear time by pivoting on the median of five-element medians, and groups of three do not yield that bound.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
