Algorithm Search⌘K

Proofs: Randomized quickselect

These are the human proofs of Randomized quickselect, at full length.

Randomized quickselect returns the item of rank k, stops, and makes at most 4n expected comparisons full

This proof shows: correctness, termination, complexity.

Let A0A_0 be the input array, n=∣A0∣≥1n = |A_0| \ge 1 and 0≤k<n0 \le k < n. Write sel(X,j)\mathrm{sel}(X, j) for the item at index jj of a sorted copy of the multiset XX. The goal is sel(A0,k)\mathrm{sel}(A_0, k). For a range we write A[a..b)A[a..b) for A[a],…,A[b−1]A[a], \ldots, A[b - 1], and X≤YX \le Y for two sets of items means x≤yx \le y for all x∈Xx \in X and y∈Yy \in Y.

Lemma 1 (PARTITION3). Let lo<hilo < hi and let pp be an item of A[lo..hi)A[lo..hi). PARTITION3 permutes A[lo..hi)A[lo..hi), leaves the rest of AA alone, makes exactly hi−lohi - lo three-way comparisons, and returns lt,gtlt, gt with lo≤lt<gt≤hilo \le lt < gt \le hi such that A[lo..lt)<pA[lo..lt) < p, A[lt..gt)=pA[lt..gt) = p and A[gt..hi)>pA[gt..hi) > p.

Proof. Each step of lines L11 to L13 only swaps items of A[lo..hi)A[lo..hi), so the multiset of the range is kept and the rest of AA is not touched. We show that the following holds at line L10: lo≤lt≤i≤gt≤hilo \le lt \le i \le gt \le hi, A[lo..lt)<pA[lo..lt) < p, A[lt..i)=pA[lt..i) = p and A[gt..hi)>pA[gt..hi) > p. At line L9 the three ranges are empty. In line L11, A[i]<pA[i] < p. If lt<ilt < i, the swap moves the item A[lt]=pA[lt] = p to index ii and the smaller item to index ltlt; if lt=ilt = i, nothing moves. Then both ltlt and ii grow by one, and the three facts hold. In line L12, A[i]>pA[i] > p; after gtgt drops by one, the swap puts this item at index gtgt, and the item from the old index gt−1gt - 1 comes to ii, still unexamined. In line L13, A[i]=pA[i] = p, and ii grows by one. Each pass compares one item with pp once, and gt−igt - i drops by exactly one, so the loop runs hi−lohi - lo times and stops with i=gti = gt. Then the three ranges cover A[lo..hi)A[lo..hi). The pivot pp is in the range and is equal to itself, so it lies in A[lt..gt)A[lt..gt), and lt<gtlt < gt. □\square

Invariant 2. At the start of each pass of the loop L2 to L8:

  1. AA is a permutation of A0A_0;
  2. A[0..lo)≤A[lo..hi)≤A[hi..n)A[0..lo) \le A[lo..hi) \le A[hi..n);
  3. lo≤k<hilo \le k < hi.

Proof. After line L1, (1) and (3) hold since 0≤k<n0 \le k < n, and (2) holds with two empty sets. Assume the invariant at the start of a pass that does not return. By Lemma 1 and (1), AA stays a permutation of A0A_0. By (2), each item left of lolo is at most each item of A[lo..hi)A[lo..hi), and PARTITION3 only permutes this range, so the inequalities of (2) still hold for the old lolo and hihi. If line L6 sets hi←lthi \leftarrow lt, then A[lt..hiold)A[lt..hi_{\mathrm{old}}) holds items ≥p\ge p and A[lo..lt)A[lo..lt) holds items <p< p, so A[lo..lt)≤A[lt..n)A[lo..lt) \le A[lt..n), and (2) holds with the new hihi. Also lo≤k<ltlo \le k < lt by the test. If line L7 sets lo←gtlo \leftarrow gt, then A[loold..gt)≤p<A[gt..hi)A[lo_{\mathrm{old}}..gt) \le p < A[gt..hi), so (2) holds with the new lolo, and gt≤k<higt \le k < hi. □\square

Lemma 3. If (1) and (2) hold and lo≤j<hilo \le j < hi, then sel(A0,j)=sel(A[lo..hi),j−lo)\mathrm{sel}(A_0, j) = \mathrm{sel}(A[lo..hi), j - lo).

Proof. By (2), the sorted copy of AA is the sorted copy of A[0..lo)A[0..lo), then that of A[lo..hi)A[lo..hi), then that of A[hi..n)A[hi..n). Its first part has lolo items. By (1) it is also the sorted copy of A0A_0. So index jj of it is index j−loj - lo of the sorted copy of A[lo..hi)A[lo..hi). □\square

Theorem 4 (correctness). If the algorithm returns, it returns sel(A0,k)\mathrm{sel}(A_0, k).

Proof. It returns at line L8 with lt≤k<gtlt \le k < gt, where Invariant 2 held at the start of the pass and Lemma 1 holds for the partition. Since the partition only permuted A[lo..hi)A[lo..hi), (1) and (2) still hold. The sorted copy of A[lo..hi)A[lo..hi) is the sorted copy of A[lo..lt)A[lo..lt), then the gt−ltgt - lt copies of pp, then the sorted copy of A[gt..hi)A[gt..hi). Since lt−lo≤k−lo<gt−lolt - lo \le k - lo < gt - lo, its item at index k−lok - lo is pp. By Lemma 3 this is sel(A0,k)\mathrm{sel}(A_0, k), and A[k]=pA[k] = p by Lemma 1. □\square

Theorem 5 (termination). For every input and every sequence of random choices, the algorithm returns after at most nn passes and at most n(n+1)/2n(n + 1)/2 three-way comparisons.

Proof. Let m=hi−lom = hi - lo, which is at least 1 by (3). By Lemma 1, lt<gtlt < gt. A pass that does not return replaces the range by A[lo..lt)A[lo..lt) or by A[gt..hi)A[gt..hi), each of which misses the non-empty A[lt..gt)A[lt..gt), so mm drops by at least 1. So there are at most nn passes, and the ranges have sizes at most n,n−1,…,1n, n - 1, \ldots, 1. A pass on a range of size mm makes mm comparisons, which gives the second bound. □\square

Theorem 6 (expected comparisons). Let C(A0,k)C(A_0, k) be the number of three-way comparisons. Then E[C(A0,k)]≤4n\mathbb{E}[C(A_0, k)] \le 4n for every input A0A_0 of size nn and every kk, where the expectation is over the random indices of line L3.

Proof. We show by strong induction on m≥1m \ge 1: for every array range of size mm that satisfies Invariant 2 at the start of a pass, the expected number of comparisons from this pass to the end is at most 4m4m. Given the range, the later passes depend only on the contents of the range and on fresh random choices.

Fix the range A[lo..hi)A[lo..hi) with mm items. Order its items by value, with ties broken by their index in the range before the pass, and let r∈{0,…,m−1}r \in \{0, \ldots, m - 1\} be the position of the chosen pivot item in this order. Line L3 picks the index uniformly, so rr is uniform. The items in A[lo..lt)A[lo..lt) are smaller than pp, so they come before the pivot in this order, and there are at most rr of them. In the same way, A[gt..hi)A[gt..hi) has at most m−1−rm - 1 - r items. So the next range, if any, has a size m′≤max⁡(r,m−1−r)<mm' \le \max(r, m - 1 - r) < m. This pass makes mm comparisons. By the induction hypothesis, the expected cost after this pass, given rr, is at most 4max⁡(r,m−1−r)4\max(r, m - 1 - r), and it is 0 when the pass returns. So

E[cost]≤m+1m∑r=0m−14max⁡(r,m−1−r)≤m+8m∑j=⌊m/2⌋m−1j,\mathbb{E}[\text{cost}] \le m + \frac{1}{m} \sum_{r = 0}^{m - 1} 4 \max(r, m - 1 - r) \le m + \frac{8}{m} \sum_{j = \lfloor m/2 \rfloor}^{m - 1} j,

since each value j=max⁡(r,m−1−r)j = \max(r, m - 1 - r) is at least ⌊m/2⌋\lfloor m/2 \rfloor and is taken by at most two values of rr. For m=2tm = 2t the last sum is t(3t−1)/2=3m2/8−m/4t(3t - 1)/2 = 3m^2/8 - m/4, and for m=2t+1m = 2t + 1 it is 3t(t+1)/2=3(m2−1)/83t(t + 1)/2 = 3(m^2 - 1)/8. In both cases it is at most 3m2/83m^2/8. So the expected cost is at most m+3m=4mm + 3m = 4m. For m=1m = 1 the pass makes 1 comparison and returns, which is at most 4. The claim for m=nm = n and the first pass is the theorem. □\square

Theorems 4, 5 and 6 together show that randomized quickselect is a Las Vegas algorithm for the selection problem with at most 4n4n expected and n(n+1)/2n(n + 1)/2 worst-case three-way comparisons, and O(1)O(1) extra space, since it stores only lolo, hihi, ii, pp, ltlt and gtgt. ■\blacksquare

Back to Randomized quickselect