Definition · Order statisticThe k-th order statistic of n values is the k-th smallest of them. The first is the minimum, the n-th is the maximum, and the median is the ⌈n/2⌉-th. Finding one is the selection problem.
Definition · QuickselectQuickselect finds the k-th smallest of a[lo..hi]. Partition the range; let q be the pivot's index and r its rank within the range. If r = k, return the pivot. If k < r, recurse on a[lo..q−1] for the same k; otherwise recurse on a[q+1..hi] for k − r. The other side is never looked at again.
Everything about the partition is unchanged, so a call on a range of m elements still costs m − 1 comparisons. What changed is that only one of the two pieces is visited.
quicksort T(n) = T(k) + T(n − k − 1) + (n − 1) two terms
quickselect T(n) = T(one side) + (n − 1) one term
if every pivot were exactly median:
T(n) = T(n/2) + n = n + n/2 + n/4 + … < 2n
if every pivot were worst:
T(n) = T(n − 1) + (n − 1) = n(n − 1)/2
Theorem · Randomised quickselect is expected linearWith the pivot of each call drawn uniformly at random from that call's range, the expected number of comparisons quickselect makes on n elements is less than 4n, for every input and every k.
Call a pivot good if its rank within the range lies in the middle half. A uniformly drawn pivot is good with probability at least one half, and a good pivot leaves a range of at most 3n/4 for the next call.
So the expected number of calls spent before the range first drops to 3n/4 or below is at most two, and each of those calls costs less than n. Charging that to a size class, the expected total is at most 2n(1 + 3/4 + 9/16 + …) = 8n, which is already linear.
The constant is loose because a bad pivot still shrinks the range. Solving T(n) ≤ (1/n)Σ T(m) + (n − 1), with the sum over the m from ⌈n/2⌉ to n − 1 that a pivot can leave, by substitution gives T(n) ≤ 4n, and that is the line the lab draws.
□
Example · The twelfth smallest of twenty-four, seed 5The array is a seeded shuffle of 1 … 24, and the twelfth smallest is 12. Three partitions find it: the range 1–24 shrinks to 12–24, then to a single element. 35 comparisons, against merge sort's 83 for sorting the array and indexing into it — a baseline counted by an implementation written for another course that has never heard of this one.
Change the seed and the count moves: 108 at seed 1, 57 at seed 2, 86 at seed 3, 35 at seed 5. Over 200 seeds the mean is 59.6 and the extremes are 23 and 118. Two of those draws are above the 4n = 96 reference line, which is allowed: 4n bounds the expectation, not each run. A reader who takes a single count as the bound will see it violated and conclude the wrong thing.
A deterministic rule fails here in the same way it failed for sorting
Take the first element as the pivot and ask for the median of an already sorted array of 24: the pivot is the smallest key every time, one element is discarded per call, and the lab measures 210 comparisons against merge sort's 52 on the same array. Selection lost, badly, and for exactly the reason “Quicksort and Its Worst Case” gave. “Median of Medians” removes the randomness instead of relying on it.
One place this matters outside the course: System Design's “Percentiles from a Sample” finds the ⌈qn⌉-th sorted value of a latency sample by sorting the sample and indexing into it. That is Θ(n log n) for a single order statistic, and selection would do it in expected linear time — the same answer, the same rank, one recursive call per level instead of a full sort.