BITS Pilani
CS F364: Design and Analysis of Algorithms

Selection: Randomized & Deterministic

Lecture 9 |2026-06-02

Selection: Finding the Median

  • Selection: Finding the Median
  • Randomized Select
  • Deterministic Select

Problem Definition

  • Given an unsorted array A of n distinct integers, find the k^{\text{th}} smallest element.
  • Can we do it faster than sorting (O(n \log n))?
  • This problem was solved in 1972 by Blum, Floyd, Pratt, Rivest, and Tarjan.
  • Today, we will study:
    • A linear-time randomized algorithm.
    • A linear-time deterministic algorithm.

Algorithm Approach

  • In RANDOMIZED-QUICKSORT, after the partition step we can tell which subarray has the item we are looking for, just by looking at their sizes.
  • So, we only need to recursively examine one subarray, not two.

Randomized Select

  • Selection: Finding the Median
  • Randomized Select
  • Deterministic Select

QuickSelect

QuickSelect Algorithm

Algorithm Analysis

Theorem

The expected number of comparisons for QuickSelect is O(n).

Proof:

  • Let T(n, k) denote the expected time to find the k^{\text{th}} smallest in an array of size n,

  • and let T(n) = \max_k T(n, k).

  • We will show that T(n) \leq 4n.

  • First, it takes n-1 comparisons to split the array into two parts. As with Quicksort, these parts might have size 0 and n-1, or 1 and n-2, and so on. Each of these is equally likely.

Analysis (Continued)

Since we are giving an upper bound, we consider the worst case and imagine we always recurse on the larger part:

T(n) \leq (n-1) + \frac{2}{n} \sum_{i=n/2}^{n-1} T(i)

Assuming T(i) \leq 4i for i < n:

\begin{align*} T(n) &\leq (n-1) + \frac{2}{n} \sum_{i=n/2}^{n-1} 4i \\ &= (n-1) + \frac{8}{n} \cdot \frac{3n^2 - 2n}{8} \\ &\leq (n-1) + 3n \\ &< 4n \end{align*}

Thus T(n) = O(n). The expected running time of QuickSelect is linear!

Deterministic Select

  • Selection: Finding the Median
  • Randomized Select
  • Deterministic Select

Deterministic Select

Problem: Given an array A of n numbers, and an integer i, find the i^{\text{th}} smallest element in A.

  • Deterministic Partition.
  • Median of median trick.

Group of 5 Medians

Algorithm: Select

Deterministic Select Algorithm

Recurrence Analysis

T(n) = T(n/5) + T(7n/10 + 6) + O(n)

  • T(n/5): Time to find the median of the \lceil n/5 \rceil medians (recursive call).
  • T(7n/10 + 6): Recursive call on the larger partition (at most 7n/10 + 6 elements).
  • O(n): Partitioning and grouping overhead.

Solving this recurrence gives T(n) = O(n).