3.6.10 · D1Sorting & Searching

Foundations — Linear time selection — median of medians algorithm

2,265 words10 min readBack to topic

Before you can read the parent note, you must own every symbol it fires at you. We build them one at a time, each anchored to a picture, each earned before use.


1. The pile: an array and its size

Picture a shelf of numbered boxes. The order they sit in is whatever we were handednot sorted. That "unsorted" fact is the whole reason the topic exists.

Figure — Linear time selection — median of medians algorithm

Why the topic needs it: every cost in the parent (like ) is measured against this . If you don't feel as "how many boxes," phrases like "throw away half the array" are meaningless.


2. Rank, and the symbol

Figure — Linear time selection — median of medians algorithm

Why the tool and not another? We could sort to read off seat — but sorting answers all seats when we asked about one. That over-answering is exactly the waste we want to avoid. This is the Order statistics view: the "-th order statistic" is a fancy name for "the value at rank ."


3. Big-O: , ,

Figure — Linear time selection — median of medians algorithm

Why the topic needs it: the whole point is " worst-case." Without a feel for these curves, that claim is empty. See Recurrence relations and the Master Theorem for how such costs are computed.


4. The pivot , its landing index , and partitioning

Figure — Linear time selection — median of medians algorithm

WHY partition at all? Once we know the pivot's landing index , we compare it to our target seat :

  • → the pivot is the answer, stop.
  • → the answer sits at a smaller rank, look only left.
  • → the answer sits at a larger rank, look only right.

We just deleted a whole side of the array with one pass. This is the engine of Quickselect and the sibling algorithm Quicksort. The mechanics of the single sweep are the topic of Partitioning (Lomuto vs Hoare).


5. Groups, medians, and "median of medians"

Figure — Linear time selection — median of medians algorithm

WHY this dance? A single "middle-of-a-group" median could still be extreme for the whole array. But the median of the medians is a middle value of middle values — it is surrounded on both sides by a guaranteed slice of the pile, so it can never be a near-extreme pivot.

Figure — Linear time selection — median of medians algorithm

6. Notation the parent throws at you


Prerequisite map

Array A and size n

Rank k = k-th smallest

Pivot p and landing index i

Selection problem

Median and group of 5

Median of medians M

3n/10 guarantee -> 7n/10 side

Big-O growth rates

Recurrence T of n

Base case n <= 5

Ceiling and floor

Linear-time Selection


Equipment checklist

Test yourself — each should feel obvious before you open the parent note.

What does stand for, and is sorted?
is the number of elements in the array ; is not sorted.
What does the target mean, and is it 0- or 1-indexed?
The rank we want; the answer is the value at seat if were sorted, counted from (1-indexed).
In one sentence, what does versus tell you?
How the work grows with pile size — linearly versus quadratically; it ignores constant factors.
What is a pivot, and what does partitioning produce?
A chosen dividing value; partitioning rearranges into smaller / equal / larger zones and fixes the pivot's final sorted index.
What is the pivot's landing index and how do you get it?
The rank the pivot occupies after partitioning; if elements are smaller than then , read straight off the partition sweep.
Why does a min/max pivot cause ?
It sheds only one element per step ( or ), so we do linear work times.
What is the median of a 5-element group?
The 3rd value after sorting the 5 (two below, two above).
What is , the median of medians, and why is it good?
The median of all the group medians; a rectangle of boxes is guaranteed on each side of it, so it can't be near-extreme.
Where does the come from?
At least elements are guaranteed on each side of , so the surviving side holds at most .
What does measure, and what is its base case?
Worst-case total work of Select on size- input; for we sort directly in constant time, .
What do and do?
Round up and round down to the nearest integer, respectively.
Why is the punchline?
Because the two recursive sub-piles together shrink the problem by a constant factor , which is exactly what forces the total work down to .