Module 9 of 39

Quickselect, medians, and order statistics

Find an order statistic by partitioning, and distinguish expected linear time from a worst-case guarantee.

If you need the smallest value in a list, scan it once and keep the smallest seen so far. That takes Θ(n) comparisons up to a constant factor. If you need the median, sorting first works, but it may do more work than the question requires. The median is an order statistic: its rank in the sorted order matters, even though we do not need the full sorted list.

Background for this page. Rank starts at one here: rank one is the smallest value. A partition puts smaller and larger values on opposite sides of a pivot. Array positions usually start at zero. Related explanations: Quicksort: partitioning, pivots, and running time · Big O, Big Omega, and Big Theta.

For [9, 2, 7, 4, 6], the sorted order is [2, 4, 6, 7, 9], so the median is 6. A selection method can locate 6 without finishing that sort. It partitions around a pivot, as quicksort does. If the pivot lands at the desired rank, we are done. If the desired rank lies to one side, we continue only on that side.

The target rank determines which side can be discarded.

# Partition and learn the pivot's final rank.
partition the values around a pivot
# The answer is the pivot if its rank is wanted.
if pivot rank equals target rank: return pivot
# Otherwise discard the side that cannot contain it.
if target rank is smaller: select from the left side
otherwise: select from the right side

The result is called quickselect. A good split leaves a much smaller side to search. With random pivot choices, its expected time is Θ(n). It may still take Θ(n²) in the worst case if it repeatedly keeps almost the whole list. An expected bound averages over random choices; it does not cap the time of an individual run.

It is also possible to guarantee linear worst-case selection. The median-of-medians method groups values, uses group medians to choose a pivot, and ensures that enough values can be discarded on each step. Its worst-case time is Θ(n). The proof is a useful piece of algorithm design, but the extra grouping and bookkeeping can make it less attractive than randomized selection in some ordinary settings. “Better bound” and “faster in this program” are not identical claims.

Minimum and maximum are simpler order statistics. If both are needed, examining values in pairs can reduce the number of comparisons compared with two independent scans. The improvement is a constant factor; the growth is still Θ(n). This reminds us that asymptotic notation does not erase every useful implementation detail.

Define the target rank carefully. For an even number of values, some uses call either middle value a median; others average the two middle values. Duplicates also matter: the rank is a position in sorted order, not necessarily a unique value. A correct selection request states which convention it uses.

Adjust the rank when moving right

Suppose a pivot has rank three in a seven-item group, and the target has rank five. Discard the two smaller items and the pivot. The target now has rank two in the right group: 5−3. Moving left leaves the target rank unchanged because no smaller positions were removed. With duplicates, use counts for smaller, equal, and greater groups. Any requested rank inside the equal block returns the pivot value. A selection request should also define what median means for an even-sized input.

Practice. In [4, 9, 1, 7, 3], you want the third-smallest value. If partitioning puts pivot 4 at rank 3, what can you return? If it puts pivot 7 at rank 4, which side still contains the answer?

Answer. With rank 3, return 4 immediately. With pivot 7 at rank 4, continue on the left side. The right side cannot contain the third-smallest value.

Try another case. Two items are smaller than the pivot, three equal it, and four exceed it. Which ranks return the pivot?

Reasoning. Ranks three through five. Rank six becomes rank one in the greater group.

View the full course and choose another topic.

Image preview

Fit