Chapter 6: Medians and Order Statistics
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.
New ideas:
- rank and order statistics,
- the generic Select 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 numbers, the lectures define
= number of elements in the set that are .
The th order statistic is the element of rank . Rank 1 is the minimum, rank the maximum.
For odd they coincide. The textbook says “the median” to mean the lower median.
2.2 Sorting Is Too Slow
Sorting and indexing works in . Selection can be done in , so it beats the comparison-sorting lower bound by never fully sorting.
2.3 The Select Skeleton
Select:
- Pick (cleverly).
- Compute , and split into ( elements) and ( elements).
- If , return . If , return Select. If , return Select.
Watch the rank shift: in you look for rank , not .
2.4 Why the Pivot Must Be Good
If is extreme (near 1 or ), each call removes only a few elements, and the time becomes . The lectures need so that is not extreme, guaranteeing a constant fraction is discarded every time.
2.5 Median of Medians
- Arrange into columns of size 5 ( columns).
- Sort each column, big elements on top (linear time: each column is constant size).
- Find the median of the column medians recursively; that is .
Columns whose median is larger than contribute their median and the two elements above it, all . Columns whose median is smaller contribute three elements .
3. Main Governing Equations and Formulas
3.1 Guaranteed Elements on Each Side
Half of the groups contribute at least 3 elements greater than , except the one group with fewer than 5 elements and the group containing itself:
The same bound holds for elements less than . So the recursive call in step 3 is on at most
Use to justify the term in the recurrence.
3.2 Select Recurrence (Lecture)
where:
- : finding the median of medians,
- : recursing on the side that is kept (after discarding at least ),
- : sorting columns and partitioning.
The master theorem does not apply (two different subproblem sizes).
3.3 Intuition for Linear Time
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 .
3.4 Proof by Induction (Lecture)
Let cover the term. True for by choosing large. Inductive step:
Done if the bracket is , which means
This holds for and (at , ).
Use this exact proof on exams. It explains why the base case is .
3.5 Proof Variant (Recitation)
The recitation proves with as the linear term:
The last step holds for large enough when .
3.6 Minimum and Maximum (Textbook)
- Minimum alone: comparisons, and this is optimal (every non-winner must lose once).
- Minimum and maximum together: at most 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.
A partitioning is “helpful” (keeps at most of the elements) with probability at least , so on average it takes about two tries per helpful step. The expected number of comparisons is less than .
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
- Define and the target rank .
- Split into columns of 5, sort each column, take the column medians.
- Recursively select the median of medians as pivot .
- Partition into (less than ) and (greater than ), compute .
- Return , recurse on with , or recurse on with .
4.2 Analyzing Select
- Count elements guaranteed on each side: at least (Section 3.1).
- Write .
- Note the master theorem does not apply.
- Guess from .
- Prove by induction with explicit constants (Section 3.4).
- Conclude (the column sorting alone is ).
5. Decision Rules / Decision Trees
5.1 Which Side to Recurse On?
? → return
? → Select
? → Select
5.2 Which Selection Algorithm?
Need a worst-case linear guarantee? → median of medians Select,
Need a fast, simple method in practice? → randomized select, expected , worst
Need only min or max? → single scan, comparisons
Need both min and max? → pairs trick, 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 and the right base range ()
6. Important Tables / Correlations Needed
6.1 Selection Algorithms
| Algorithm | Pivot | Worst case | Expected |
|---|---|---|---|
| Sort then index | none | ||
| Randomized select | random | ||
| Select (median of medians) | median of column medians |
6.2 Median-of-Medians Numbers
| Quantity | Value |
|---|---|
| Column size | 5 |
| Number of columns | |
| Elements guaranteed (and ) | |
| Largest recursive call | |
| Base case in the lecture proof | |
| Constant condition |
7. Key Takeaways
- = number of elements ; lower median has rank .
- Selection runs in , faster than sorting.
- Select skeleton: pivot, partition into and , recurse on one side; use in .
- A bad pivot gives .
- Median of medians with columns of 5 guarantees at least elements on each side.
- Recurrence: , with for .
- The master theorem does not apply; prove by induction.
- The proof closes for and .
- Intuition: .
- Randomized select: expected , worst case .