Skip to content
Ahmet Çelik

Chapter 6: Medians and Order Statistics

COMP305 7 minute read

Source files used: Medians_induction (Lecture 2 notes, pages 8 to 10), Matrix Multiplication (Recitation 1, Section 3.3); textbook Chapter 9 (chap9) as support. No worked examples are included.

1. Big Picture

Median finding is the lecture’s second divide-and-conquer example: find the element of a given rank without sorting.

The Convex Hull guide split the input into two equal halves. This guide splits around a pivot, recurses on only one side, and needs a clever pivot so that side is never too big.

Pick a good pivot (median of medians), recurse on one side⇒Θ(n) worst case.\boxed{\text{Pick a good pivot (median of medians), recurse on one side}\Rightarrow\Theta(n)\text{ worst case.}}

New ideas:

  • rank and order statistics,
  • the generic Select(S,i)(S,i) skeleton,
  • columns of 5 and the median of medians,
  • an unequal recurrence solved by induction, not the master theorem,
  • textbook context: min/max, randomized select.

2. Core Ideas

2.1 Rank and Medians

Given a set of nn numbers, the lectures define

rank(x)\text{rank}(x) = number of elements in the set that are ≤x\le x.

The iith order statistic is the element of rank ii. Rank 1 is the minimum, rank nn the maximum.

lower median: rank ⌊n+12⌋,upper median: rank ⌈n+12⌉\text{lower median: rank }\left\lfloor\frac{n+1}{2}\right\rfloor,\qquad\text{upper median: rank }\left\lceil\frac{n+1}{2}\right\rceil

For odd nn they coincide. The textbook says “the median” to mean the lower median.


2.2 Sorting Is Too Slow

Sorting and indexing works in Θ(nlg⁡n)\Theta(n\lg n). Selection can be done in Θ(n)\Theta(n), so it beats the Ω(nlg⁡n)\Omega(n\lg n) comparison-sorting lower bound by never fully sorting.


2.3 The Select Skeleton

Select(S,i)(S,i):

  1. Pick x∈Sx\in S (cleverly).
  2. Compute k=rank(x)k=\text{rank}(x), and split into B={y∈S∣y<x}B=\{y\in S\mid y<x\} (k−1k-1 elements) and C={y∈S∣y>x}C=\{y\in S\mid y>x\} (n−kn-k elements).
  3. If k=ik=i, return xx. If k>ik>i, return Select(B,i)(B,i). If k<ik<i, return Select(C,i−k)(C,i-k).

Watch the rank shift: in CC you look for rank i−ki-k, not ii.


2.4 Why the Pivot Must Be Good

If rank(x)\text{rank}(x) is extreme (near 1 or nn), each call removes only a few elements, and the time becomes T(n−1)+Θ(n)=Θ(n2)T(n-1)+\Theta(n)=\Theta(n^2). The lectures need xx so that rank(x)\text{rank}(x) is not extreme, guaranteeing a constant fraction is discarded every time.


2.5 Median of Medians

  1. Arrange SS into columns of size 5 (⌈n/5⌉\lceil n/5\rceil columns).
  2. Sort each column, big elements on top (linear time: each column is constant size).
  3. Find the median of the column medians recursively; that is xx.

Columns whose median is larger than xx contribute their median and the two elements above it, all >x>x. Columns whose median is smaller contribute three elements <x<x.


3. Main Governing Equations and Formulas

3.1 Guaranteed Elements on Each Side

Half of the ⌈n/5⌉\lceil n/5\rceil groups contribute at least 3 elements greater than xx, except the one group with fewer than 5 elements and the group containing xx itself:

#{y>x}≥3(⌈n10⌉−2)≥3n10−6\#\{y>x\}\ge3\left(\left\lceil\frac{n}{10}\right\rceil-2\right)\ge\frac{3n}{10}-6

The same bound holds for elements less than xx. So the recursive call in step 3 is on at most

n−(3n10−6)=7n10+6 elementsn-\left(\frac{3n}{10}-6\right)=\frac{7n}{10}+6\text{ elements}

Use to justify the 7n/10+67n/10+6 term in the recurrence.


3.2 Select Recurrence (Lecture)

T(n)={O(1)n≤140T ⁣(⌈n/5⌉)+T ⁣(7n10+6)+Θ(n)n>140T(n)=\begin{cases}O(1) & n\le140\\[4pt] T\!\left(\left\lceil n/5\right\rceil\right)+T\!\left(\dfrac{7n}{10}+6\right)+\Theta(n) & n>140\end{cases}

where:

  • T(⌈n/5⌉)T(\lceil n/5\rceil): finding the median of medians,
  • T(7n/10+6)T(7n/10+6): recursing on the side that is kept (after discarding at least 3n/10−63n/10-6),
  • Θ(n)\Theta(n): sorting columns and partitioning.

The master theorem does not apply (two different subproblem sizes).


3.3 Intuition for Linear Time

n5+7n10=9n10<n\frac{n}{5}+\frac{7n}{10}=\frac{9n}{10}<n

The total input size of the subproblems shrinks by a constant factor each level, so the level costs form a decreasing geometric series dominated by the root’s Θ(n)\Theta(n).


3.4 Proof T(n)≤cnT(n)\le cn by Induction (Lecture)

Let anan cover the Θ(n)\Theta(n) term. True for n≤140n\le140 by choosing cc large. Inductive step:

T(n)≤c⌈n5⌉+c(7n10+6)+anT(n)\le c\left\lceil\frac{n}{5}\right\rceil+c\left(\frac{7n}{10}+6\right)+an ≤cn5+c+7nc10+6c+an\le\frac{cn}{5}+c+\frac{7nc}{10}+6c+an =cn+(−cn10+7c+an)=cn+\left(-\frac{cn}{10}+7c+an\right)

Done if the bracket is ≤0\le0, which means

c≥70cn+10ac\ge\frac{70c}{n}+10a

This holds for n≥140n\ge140 and c≥20ac\ge20a (at n≥140n\ge140, 70c/n≤c/270c/n\le c/2).

Use this exact proof on exams. It explains why the base case is n≤140n\le140.


3.5 Proof Variant (Recitation)

The recitation proves T(n)≤dnT(n)\le dn with cncn as the linear term:

T(n)≤d(n5+1)+d(7n10+6)+cn=910dn+7d+cn≤dnT(n)\le d\left(\frac{n}{5}+1\right)+d\left(\frac{7n}{10}+6\right)+cn=\frac{9}{10}dn+7d+cn\le dn

The last step holds for large enough nn when d>10cd>10c.


3.6 Minimum and Maximum (Textbook)

  • Minimum alone: n−1n-1 comparisons, and this is optimal (every non-winner must lose once).
  • Minimum and maximum together: at most 3⌊n/2⌋3\lfloor n/2\rfloor comparisons, by processing elements in pairs (compare the pair, then smaller vs current min and larger vs current max).

3.7 Randomized Select (Textbook)

Partition around a random pivot, recurse on one side.

expected time Θ(n),worst case T(n)=T(n−1)+Θ(n)=Θ(n2)\text{expected time }\Theta(n),\qquad\text{worst case }T(n)=T(n-1)+\Theta(n)=\Theta(n^2)

A partitioning is “helpful” (keeps at most 3/43/4 of the elements) with probability at least 1/21/2, so on average it takes about two tries per helpful step. The expected number of comparisons is less than 8n8n.

This is the practical choice; the textbook calls the median-of-medians Select mostly of theoretical interest.


4. Problem-Solving Workflow

4.1 Describing Select

  1. Define rank(x)\text{rank}(x) and the target rank ii.
  2. Split into columns of 5, sort each column, take the column medians.
  3. Recursively select the median of medians as pivot xx.
  4. Partition into BB (less than xx) and CC (greater than xx), compute k=rank(x)k=\text{rank}(x).
  5. Return xx, recurse on BB with ii, or recurse on CC with i−ki-k.

4.2 Analyzing Select

  1. Count elements guaranteed on each side: at least 3n/10−63n/10-6 (Section 3.1).
  2. Write T(n)=T(⌈n/5⌉)+T(7n/10+6)+Θ(n)T(n)=T(\lceil n/5\rceil)+T(7n/10+6)+\Theta(n).
  3. Note the master theorem does not apply.
  4. Guess O(n)O(n) from n/5+7n/10<nn/5+7n/10<n.
  5. Prove T(n)≤cnT(n)\le cn by induction with explicit constants (Section 3.4).
  6. Conclude Θ(n)\Theta(n) (the column sorting alone is Ω(n)\Omega(n)).

5. Decision Rules / Decision Trees

5.1 Which Side to Recurse On?

k=ik=i? → return xx

k>ik>i? → Select(B,i)(B,i)

k<ik<i? → Select(C,i−k)(C,i-k)

5.2 Which Selection Algorithm?

Need a worst-case linear guarantee? → median of medians Select, Θ(n)\Theta(n)

Need a fast, simple method in practice? → randomized select, expected Θ(n)\Theta(n), worst Θ(n2)\Theta(n^2)

Need only min or max? → single scan, n−1n-1 comparisons

Need both min and max? → pairs trick, 3⌊n/2⌋3\lfloor n/2\rfloor comparisons

5.3 Proving the Recurrence

Recurrence has two different subproblem sizes? → no master theorem: substitution (induction)

Proof does not close? → check the constants: need c≥20ac\ge20a and the right base range (n≤140n\le140)


6. Important Tables / Correlations Needed

6.1 Selection Algorithms

AlgorithmPivotWorst caseExpected
Sort then indexnoneΘ(nlg⁡n)\Theta(n\lg n)Θ(nlg⁡n)\Theta(n\lg n)
Randomized selectrandomΘ(n2)\Theta(n^2)Θ(n)\Theta(n)
Select (median of medians)median of column mediansΘ(n)\Theta(n)Θ(n)\Theta(n)

6.2 Median-of-Medians Numbers

QuantityValue
Column size5
Number of columns⌈n/5⌉\lceil n/5\rceil
Elements guaranteed >x>x (and <x<x)≥3(⌈n/10⌉−2)≥3n/10−6\ge3(\lceil n/10\rceil-2)\ge3n/10-6
Largest recursive call7n/10+67n/10+6
Base case in the lecture proofn≤140n\le140
Constant conditionc≥20ac\ge20a

7. Key Takeaways

  • rank(x)\text{rank}(x) = number of elements ≤x\le x; lower median has rank ⌊(n+1)/2⌋\lfloor(n+1)/2\rfloor.
  • Selection runs in Θ(n)\Theta(n), faster than sorting.
  • Select skeleton: pivot, partition into BB and CC, recurse on one side; use i−ki-k in CC.
  • A bad pivot gives T(n−1)+Θ(n)=Θ(n2)T(n-1)+\Theta(n)=\Theta(n^2).
  • Median of medians with columns of 5 guarantees at least 3n/10−63n/10-6 elements on each side.
  • Recurrence: T(⌈n/5⌉)+T(7n/10+6)+Θ(n)T(\lceil n/5\rceil)+T(7n/10+6)+\Theta(n), with O(1)O(1) for n≤140n\le140.
  • The master theorem does not apply; prove T(n)≤cnT(n)\le cn by induction.
  • The proof closes for n≥140n\ge140 and c≥20ac\ge20a.
  • Intuition: n/5+7n/10<nn/5+7n/10<n.
  • Randomized select: expected Θ(n)\Theta(n), worst case Θ(n2)\Theta(n^2).