Finding the Median Without Fully Sorting: Linear-Time Selection Algorithms

Finding the k-th smallest element in an unsorted array does not require the full Θ(n log n) cost of sorting; it can be done in linear time. This comprehensive guide covers the trivial case of finding the minimum or maximum, an elegant randomized selection algorithm with linear expected time, and a more intricate deterministic algorithm that guarantees linear time even in the worst case.

Order StatisticsSelection AlgorithmMedian of Medians

~6 دقیقه مطالعه · آخرین به‌روزرسانی ۱۶ شهریور ۱۴۰۵

The Selection Problem

The i-th Order Statistic of a set of n elements is simply the i-th smallest element in that set. The Selection Problem asks: given an unsorted array and an index i, find the i-th order statistic. Special cases include finding the minimum (i = 1), the maximum (i = n), and the median (i = ⌈n/2⌉).

A naive approach would sort the array first, using one of the Θ(n log n) algorithms discussed earlier in this series, and then simply index into the sorted result. This works but does more work than necessary — sorting solves a strictly harder problem than finding one specific order statistic. This article covers algorithms that solve selection directly, without fully sorting.

Finding the Minimum or Maximum: A Trivial Linear Scan

Finding either the minimum or the maximum alone requires only a single pass through the array, comparing each element against the current best candidate.

MINIMUM(A, n):
  min = A[1]
  for i = 2 to n:
      if A[i] < min:
          min = A[i]
  return min

This clearly runs in Θ(n) time, and it is easy to prove this is optimal: any algorithm that finds the minimum must examine every element at least once, since an unexamined element could always turn out to be the true minimum, giving a matching Ω(n) lower bound.

A more interesting question is finding both the minimum and maximum simultaneously. A naive approach makes two separate passes, using 2n - 2 comparisons total. A cleverer approach processes elements in pairs, comparing each pair against each other first, then comparing only the winner against the current maximum and only the loser against the current minimum, reducing the total to approximately 3n/2 comparisons — a meaningful constant-factor improvement.

Randomized Selection: Linear Expected Time

Finding an arbitrary order statistic i is more interesting. RANDOMIZED-SELECT adapts the partitioning idea from randomized quicksort, discussed earlier in this series, but with a crucial difference: after partitioning, it recurses into only one side rather than both.

RANDOMIZED-SELECT(A, p, r, i):
  if p == r:
      return A[p]
  q = RANDOMIZED-PARTITION(A, p, r)
  k = q - p + 1   // number of elements in the low side, including pivot
  if i == k:
      return A[q]         // the pivot is exactly the answer
  elif i < k:
      return RANDOMIZED-SELECT(A, p, q - 1, i)   // recurse left only
  else:
      return RANDOMIZED-SELECT(A, q + 1, r, i - k) // recurse right only

Because the algorithm only recurses into one side of the partition rather than both, it avoids the extra recursive work that made quicksort's total cost Θ(n log n). Intuitively, this halves (or more) the problem size at each step while doing only Θ(n) partitioning work at the current step, rather than Θ(n) work at every level as quicksort does across both branches.

Analyzing the Expected Running Time

Using the same style of probabilistic analysis discussed earlier in this series, the expected running time can be shown to satisfy:

Since the pivot is chosen randomly, in expectation
the partition splits roughly evenly, giving a
recurrence of approximately:

E[T(n)] ≤ E[T(n/2)] + O(n)

Solving this recurrence, similar in form to the
master method's Case 2 discussed earlier in this series:

E[T(n)] = O(n)

A more rigorous derivation, accounting for all possible partition splits weighted by their probability, confirms this O(n) expected bound holds regardless of the value of i requested, including the worst cases of finding the minimum, maximum, or median.

Like randomized quicksort, this algorithm still has a Θ(n²) worst case — for instance, if the randomly chosen pivot happens to repeatedly be the smallest or largest remaining element — but this worst case is vanishingly unlikely across random choices, making the algorithm reliably fast in practice.

Deterministic Selection: Guaranteed Linear Time

For applications requiring a guaranteed worst-case linear time bound, without any dependence on randomization, a more elaborate deterministic algorithm exists, often called the Median-of-Medians algorithm. Its key innovation is a clever method for choosing a pivot guaranteed to produce a reasonably balanced partition, no matter what the input looks like.

SELECT(A, n, i):
  if n ≤ some small constant (e.g. 5):
      sort A directly and return the i-th element
  
  Divide A into ⌈n/5⌉ groups of 5 elements each
  Find the median of each group (by sorting each small group)
  Recursively find the median of these ⌈n/5⌉ medians — call it x
  
  Partition A around x
  Let k = rank of x in the partitioned array
  if i == k:
      return x
  elif i < k:
      recursively SELECT on the low side for the i-th element
  else:
      recursively SELECT on the high side for the (i-k)-th element

The critical insight is that the median-of-medians x is guaranteed to be greater than at least roughly 3n/10 elements and less than at least roughly 3n/10 elements, ensuring the partition is never too lopsided, regardless of the specific input.

Why the Recurrence Solves to Linear Time

The algorithm makes two recursive calls: one on ⌈n/5⌉ elements to find the median of medians, and one on at most roughly 7n/10 elements for the main recursive selection step, plus O(n) work for grouping, sorting small groups, and partitioning.

T(n) ≤ T(⌈n/5⌉) + T(7n/10) + O(n)

Solving this recurrence using the substitution method discussed earlier in this series, guessing T(n) ≤ cn:

T(n) ≤ c⌈n/5⌉ + c(7n/10) + O(n)
     ≤ cn/5 + c + 7cn/10 + O(n)
     = 9cn/10 + c + O(n)

This is ≤ cn provided c is chosen large enough
that the O(n) term and the +c are absorbed
by the remaining cn/10 slack

Therefore T(n) = O(n)

This confirms the median-of-medians algorithm achieves Θ(n) worst-case running time — a genuinely deterministic linear-time guarantee, unlike the randomized algorithm's expected-time guarantee.

Comparing the Two Selection Algorithms

Randomized Select:
  - Expected time: O(n)
  - Worst-case time: O(n²), though extremely unlikely
  - Simple to implement, small constant factors
  - Preferred in most practical situations

Median-of-Medians (deterministic Select):
  - Worst-case time: O(n), guaranteed
  - More complex to implement, larger constant factors
  - Preferred when worst-case guarantees are essential,
    such as in real-time systems or adversarial settings

Why Selection Algorithms Matter Beyond the Median

Efficient selection has practical applications throughout computer science: finding percentiles in statistical analysis, identifying the k-th shortest path in network routing, and as a subroutine within other algorithms, including a variant used to choose better pivots for quicksort itself in performance-critical implementations. The fact that this problem admits a genuinely linear-time solution, strictly faster than the Ω(n log n) lower bound that applies to full sorting, illustrates an important principle in algorithm design: understanding exactly what a problem requires, rather than reaching for the most

نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Number-Theoretic Algorithms: GCD, Modular Exponentiation, and RSA

Modern cryptography and countless algorithmic applications rely on a handful of elegant number-theoretic algorithms. This comprehensive guide covers Euclid's algorithm for computing the greatest common divisor, fast modular exponentiation for efficiently computing large powers, and the mathematical foundation of RSA encryption, one of the most widely deployed cryptographic systems in the world.

ادامه

Computational Geometry Basics: Orientation, Line Intersection, and Convex Hull

Geometric algorithms solve problems involving points, lines, and shapes, appearing in computer graphics, robotics path planning, and geographic information systems. This comprehensive guide covers the cross-product-based orientation test that underlies nearly every geometric algorithm, segment intersection detection built on that test, and Graham's scan algorithm for computing the convex hull of a set of points.

ادامه

String Matching Algorithms: Naive Search, Rabin-Karp, and Beyond

Searching for a pattern within a larger text is one of the most common operations in computing, from text editors to DNA sequence analysis. This comprehensive guide covers the naive string-matching algorithm and its quadratic worst case, then explains the Rabin-Karp algorithm's clever use of hashing to achieve fast average-case performance, including how it handles hash collisions correctly.

ادامه

Approximation Algorithms: Getting Provably Close to Optimal for Hard Problems

When a problem is proven NP-complete, an exact efficient solution is unlikely to exist, but that does not mean giving up on the problem entirely. This comprehensive guide explains approximation algorithms, which sacrifice guaranteed optimality for guaranteed efficiency, covering the vertex cover and traveling salesman problems as classic examples with provable approximation ratios.

ادامه

NP-Completeness Explained: P, NP, and Why Some Problems Resist Efficient Solutions

Some problems have resisted every attempt at an efficient algorithm for decades, yet no one has proven an efficient solution is impossible. This comprehensive guide explains the classes P and NP, the concept of polynomial-time reductions used to compare problem difficulty, and how proving a problem NP-complete provides strong evidence, though not proof, that no efficient algorithm exists.

ادامه

Maximum Flow: Ford-Fulkerson and the Min-Cut Max-Flow Theorem

Maximum flow problems model the largest possible throughput through a network with capacity-limited connections, from water pipes to data networks. This comprehensive guide introduces flow networks, walks through the Ford-Fulkerson method for finding maximum flow using augmenting paths, and explains the elegant min-cut max-flow theorem that connects two seemingly different problems into one.

ادامه