QuickSelect Algorithm
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.
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!
Problem: Given an array A of n numbers, and an integer i, find the i^{\text{th}} smallest element in A.
Deterministic Select Algorithm
T(n) = T(n/5) + T(7n/10 + 6) + O(n)
Solving this recurrence gives T(n) = O(n).
CS F364: Design & Analysis of AlgorithmsTulasimohan Molli