Abstract

For theoretical computer scientists, it is unsatisfactory to only have a randomized algorithm (QuickSelect) that could run in quadratic time. Blum, Floyd, Pratt, Rivest, and Tarjan developed a deterministic approach to finding the median (or any smallest element), guaranteeing worst-case linear time.

  • Category: Divide and Conquer / Selection (Deterministic)
  • Input: A list , and an integer
  • Output: The [smallest] element of
  • Paradigm: Deterministic Divide and Conquer
  • Typical use cases: any selection scenario needing a guaranteed worst-case bound rather than an expected one — adversarial inputs, hard real-time systems, or as a worst-case-safe pivot-picker inside other algorithms (e.g. a guaranteed- Quick Sort)

Core Logic (High-Level)

QuickSelect’s randomized pivot works well on average, but a run of bad luck can still cost . The fix: instead of hoping for a decent pivot, construct one that’s provably decent every time.

  1. Split the list into groups of 5.
  2. Find the median of each group (sort each tiny group of 5, or recursively call MofM(S[i], 3)).
  3. Recursively find the median of those group-medians — this is the “median of medians,” , used as the pivot.
  4. Partition around into , (equal to ), — exactly like Partition with Pivot in QuickSelect.
  5. Recurse into whichever side contains rank (adjusting as needed), just like QuickSelect.

Key Idea

Because is the median of group-medians, at least half of those groups have a median — and for each such group, at least 3 of its 5 elements (the median and the two below it) are also . That guarantees roughly elements are provably (and symmetrically, are provably ) — no matter how the input is arranged. This turns “hope for a lucky pivot” into “guarantee a decent one,” at the cost of doing extra work to compute it.


Pseudocode (Mid-Level Implementation)

Algorithm 8 Median of Medians

Input: LL list of elements

Input: kk the kthk^{th} smallest element to find

Output: the kthk^th element

procedure MofM(L,kL, k)

if LL has 1010 or fewer elements then

Sort(LL)

return kthk^{th} element

Partition LL into sublists S[i]S[i] of five elements each

for i=1,,n5i = 1, \dots, \frac{n}{5} do

m[i]=MofM(S[i],3)m[i] = MofM(S[i], 3)

M=MofM([m[1],,m[n5]],n10)M = MofM([m[1], \dots, m[\frac{n}{5}]], \frac{n}{10})

Split the list into sets SLSL, SMSM, SRSR

if kSLk \leq |SL| then

return MofM(SL,k)MofM(SL, k)

if kSL+Svk \leq |SL| + |Sv| then

return vv

else

return MofM(SR,kSLSv)MofM(SR, k - |SL| - |Sv|)

Reading This Pseudocode

The split step defines (elements equal to the pivot ), but the branches below it reference Sv and v — these are presumably meant to be SM and M respectively (likely copied over from the QuickSelect pseudocode without renaming). Treat Sv = SM and v = M when reading this.

Variables & Data Structures

NameTypePurpose
S[i]Sublists of 5The list partitioned into groups of 5 elements each
m[i]Array of group mediansThe median (3rd of 5) of each group S[i], found via a recursive call
MElementThe “median of medians” — the recursively-found median of the m[i] array; used as the pivot
SL, SM, SRSublistsElements smaller than, equal to, and greater than M, from partitioning around it

Helper Functions / Operations Used

  • Sort (base case only) — sorting a list of elements; since the size is bounded by a constant.
  • MofM(S[i], 3) — recursively finds the median of a 5-element group by treating “median of 5” as its own selection instance.
  • Partition around M — same in-place partitioning idea as Partition with Pivot.

Proof of Correctness

The recursive correctness argument (base case + correctly identifying which of // contains rank , adjusting appropriately) mirrors QuickSelect’s proof exactly, since the partition-and-recurse structure is identical — the only difference is how the pivot is chosen. The genuinely new thing to prove here is that the chosen pivot is always good enough:

Claim: and .

Proof: Consider the group-medians. Since is their median, at least half of them — groups — have a group-median . For each such group, since its median is , at least 3 of its 5 elements (the median itself, plus the two elements below it in that group) are also . So at least elements of are provably (ignoring at most one partial leftover group, which only affects the bound by a bounded constant).

That means at most elements can be , so . The symmetric argument (using the groups with median ) gives .


Time & Space Complexity Analysis

The Recurrence

By construction, and (proven above), so no matter which side we recurse on:

— the term from finding the median of medians, the term from the worst-case recursive selection call, and for partitioning and the group-median computations.

You cannot use the Master Theorem here — it only applies to a single recursive term of the form , not a sum of two differently-sized recursive calls like this. Instead, this is solved directly by induction.

Proof by Induction:

Claim: there exists a constant such that for all .

  • Base case: for , the algorithm just sorts directly, so for any large enough to dominate that constant.
  • Inductive hypothesis: suppose for all .
  • Inductive step: let (for some constant ) bound the non-recursive work (partitioning, computing group medians, etc.) at this level. Then:

We want this :

So choosing (or any constant at least that large, and large enough to also cover the base case) makes the induction go through for every . Therefore:

Deterministic Selection runs in worst-case linear time.

Why groups of 5, specifically?

The group size isn’t arbitrary. With groups of size 5, the two recursive fractions sum to — strictly less than 1 is what makes the induction above work (the term has “room” to be absorbed). Groups of 3 would instead give a recurrence like , where the fractions sum to exactly 1 — that no longer converges to linear time (it degrades toward instead), since there’s no leftover fraction to absorb the work at each level.

General Case

ComplexityNotes
Time worst caseProven via the induction above — no randomness needed, unlike QuickSelect
SpaceAuxiliary storage for the groups of 5 and the array of group-medians at each level of recursion

Best / Worst / Average Case

  • Best / Worst / Average case: all — this is the entire point of the algorithm. Unlike QuickSelect, there’s no input arrangement or unlucky randomness that can push this above linear time.

Drawbacks / Constraints

  • Larger constant factor than QuickSelect. Sorting every group of 5, recursively finding the median of medians, and doing two recursive-sized calls worth of bookkeeping per level adds substantially more overhead per element than QuickSelect’s simple random pivot pick — in practice, QuickSelect is usually faster on typical (non-adversarial) inputs despite its worse worst case.
  • More complex to implement correctly — the nested recursive structure (recursing both to find group medians and to find the median of medians and to recurse into /) is easy to get subtly wrong compared to QuickSelect’s single recursive call.
  • Not suitable for: everyday use where average-case performance is what matters — reach for QuickSelect unless you specifically need a worst-case guarantee.

References / Links