Skip to content
Part IA Lent Term

Order Statistics and Median

Overview

The ii-th order statistic of a set of nn elements is the ii-th smallest element. The minimum is the 1st order statistic (i=1i = 1); the maximum is the nn-th; the median (lower median) is the (n+1)/2\lfloor (n+1)/2 \rfloor-th.

The selection problem: given a set AA of nn distinct numbers and an integer ii where 1in1 \le i \le n, find the element larger than exactly i1i-1 others.

Finding Minimum and Maximum

A simple linear scan finds the minimum (or maximum) in n1n-1 comparisons:

min = A[1]
for j = 2 to n:
    if A[j] < min: min = A[j]

Finding both minimum and maximum simultaneously can be done in 3n/22\lceil 3n/2 \rceil - 2 comparisons: process elements in pairs, compare within each pair, then compare the larger against the running maximum and the smaller against the running minimum.

TaskComparisons
Find min onlyn1n - 1
Find max onlyn1n - 1
Find min and max naively2n22n - 2
Find min and max optimally3n/22\lceil 3n/2 \rceil - 2

Quickselect

Quickselect diagram

Quickselect uses the same partition subroutine as quicksort, but only recurses on the side containing the desired order statistic:

  1. Call PARTITION(A, p, r) which rearranges A[p..r] around a pivot and returns pivot index qq.
  2. Let k=qp+1k = q - p + 1 (the rank of the pivot within the subarray).
  3. If i=ki = k, return A[q] — the pivot is the answer.
  4. If i<ki < k, recurse on A[p..q-1] for the ii-th statistic.
  5. If i>ki > k, recurse on A[q+1..r] for the (ik)(i-k)-th statistic.

Complexity

  • Expected: Θ(n)\Theta(n). Each partition is Θ(m)\Theta(m) on a subarray of size mm, but the subarray shrinks by a constant factor in expectation.
  • Worst case: Θ(n2)\Theta(n^2), when the pivot is always the minimum or maximum (e.g. already-sorted or reverse-sorted input).

Improvements

  1. Randomised pivot: Choose the pivot at random; makes worst-case inputs unlikely.
  2. Median-of-three: Pick three elements, use their median as pivot. Guarantees at least one element on each side, reducing the worst-case probability from 2/n\sim 2/n to 2/n2\sim 2/n^2.
  3. Three-way partition: Split into << pivot, == pivot, >> pivot. Excellent when many duplicates exist.

Median-of-Medians (Blum-Floyd-Pratt-Rivest-Tarjan)

This deterministic algorithm finds the ii-th order statistic in O(n)O(n) worst-case time:

  1. Divide the nn elements into groups of 5. Find the median of each group (constant-time per group via insertion sort on 5 elements).
  2. Recursively find the median of these n/5\lceil n/5 \rceil medians; use it as the pivot.
  3. Partition around this median-of-medians pivot.
  4. Recurse on the appropriate side.

Why O(n)O(n): At least 3(12n/52)3n/1063(\lceil \frac{1}{2} \lceil n/5 \rceil \rceil - 2) \ge 3n/10 - 6 elements are guaranteed to be greater than the pivot, and symmetrically for less than. So even the larger side is at most 7n/10+67n/10 + 6 elements. The recurrence:

T(n)=T(n/5)+T(7n/10+6)+Θ(n)=O(n)T(n) = T(\lceil n/5 \rceil) + T(7n/10 + 6) + \Theta(n) = O(n)

For n140n \ge 140, the constant factor works out; for smaller nn, any constant-time method suffices.

Practical Considerations

For most applications, simply sorting the array in Θ(nlogn)\Theta(n \log n) and reading the ii-th element is fast enough and far simpler to implement. Quickselect (randomised) is used in practice when nn is large and sorting overhead is undesirable.

Summary

MethodTime (expected)Time (worst case)
Sort then pick ii-thΘ(nlogn)\Theta(n \log n)Θ(nlogn)\Theta(n \log n)
Quickselect (randomised)Θ(n)\Theta(n)Θ(n2)\Theta(n^2)
Median-of-mediansΘ(n)\Theta(n)Θ(n)\Theta(n)
Find min or maxΘ(n)\Theta(n)Θ(n)\Theta(n)
Find min and maxΘ(n)\Theta(n)Θ(n)\Theta(n)