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 be the input array, and . Write for the item at index of a sorted copy of the multiset . The goal is . For a range we write for , and for two sets of items means for all and .
Lemma 1 (PARTITION3). Let and let be an item of . PARTITION3 permutes , leaves the rest of alone, makes exactly three-way comparisons, and returns with such that , and .
Proof. Each step of lines L11 to L13 only swaps items of , so the multiset of the range is kept and the rest of is not touched. We show that the following holds at line L10: , , and . At line L9 the three ranges are empty. In line L11, . If , the swap moves the item to index and the smaller item to index ; if , nothing moves. Then both and grow by one, and the three facts hold. In line L12, ; after drops by one, the swap puts this item at index , and the item from the old index comes to , still unexamined. In line L13, , and grows by one. Each pass compares one item with once, and drops by exactly one, so the loop runs times and stops with . Then the three ranges cover . The pivot is in the range and is equal to itself, so it lies in , and .
Invariant 2. At the start of each pass of the loop L2 to L8:
- is a permutation of ;
- ;
- .
Proof. After line L1, (1) and (3) hold since , 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), stays a permutation of . By (2), each item left of is at most each item of , and PARTITION3 only permutes this range, so the inequalities of (2) still hold for the old and . If line L6 sets , then holds items and holds items , so , and (2) holds with the new . Also by the test. If line L7 sets , then , so (2) holds with the new , and .
Lemma 3. If (1) and (2) hold and , then .
Proof. By (2), the sorted copy of is the sorted copy of , then that of , then that of . Its first part has items. By (1) it is also the sorted copy of . So index of it is index of the sorted copy of .
Theorem 4 (correctness). If the algorithm returns, it returns .
Proof. It returns at line L8 with , where Invariant 2 held at the start of the pass and Lemma 1 holds for the partition. Since the partition only permuted , (1) and (2) still hold. The sorted copy of is the sorted copy of , then the copies of , then the sorted copy of . Since , its item at index is . By Lemma 3 this is , and by Lemma 1.
Theorem 5 (termination). For every input and every sequence of random choices, the algorithm returns after at most passes and at most three-way comparisons.
Proof. Let , which is at least 1 by (3). By Lemma 1, . A pass that does not return replaces the range by or by , each of which misses the non-empty , so drops by at least 1. So there are at most passes, and the ranges have sizes at most . A pass on a range of size makes comparisons, which gives the second bound.
Theorem 6 (expected comparisons). Let be the number of three-way comparisons. Then for every input of size and every , where the expectation is over the random indices of line L3.
Proof. We show by strong induction on : for every array range of size that satisfies Invariant 2 at the start of a pass, the expected number of comparisons from this pass to the end is at most . Given the range, the later passes depend only on the contents of the range and on fresh random choices.
Fix the range with items. Order its items by value, with ties broken by their index in the range before the pass, and let be the position of the chosen pivot item in this order. Line L3 picks the index uniformly, so is uniform. The items in are smaller than , so they come before the pivot in this order, and there are at most of them. In the same way, has at most items. So the next range, if any, has a size . This pass makes comparisons. By the induction hypothesis, the expected cost after this pass, given , is at most , and it is 0 when the pass returns. So
since each value is at least and is taken by at most two values of . For the last sum is , and for it is . In both cases it is at most . So the expected cost is at most . For the pass makes 1 comparison and returns, which is at most 4. The claim for and the first pass is the theorem.
Theorems 4, 5 and 6 together show that randomized quickselect is a Las Vegas algorithm for the selection problem with at most expected and worst-case three-way comparisons, and extra space, since it stores only , , , , and .