Proofs: Quickselect
These are the human proofs of Quickselect, at full length.
Quickselect returns the k-th smallest item full
This proof shows: correctness, termination.
Let be the multiset of the items of the input, and let be in sorted order. The claim is: for , QUICKSELECT stops and returns , and at the end is a permutation of with , each item of at most , and each item of at least .
Lemma (PARTITION). Assume , and let be the item at the start. PARTITION permutes , changes no other entry, and returns with , , each item of at most , and each item of greater than .
Proof. Line L10 sets . The loop of line L12 keeps this invariant at the start of the iteration for : , each item of is at most , each item of is greater than , and .
- Initialization. Line L11 sets and the first value is , so both parts are empty.
- Maintenance. If , the loop only increases , so joins the part of the greater items. If , line L14 swaps with , which is the first item of the greater part (or itself if that part is empty). Then and the greater part moves one place to the right, so after line L15 both statements hold for . The index stays below , so does not change.
- Termination. The loop runs for , so it ends with . Then and . Line L16 swaps with . If , the item moved to is greater than , so . If , the swap does nothing. In both cases the lemma holds with .
Each step is a swap of two entries of , so the range is permuted and no other entry changes.
Invariant of the main loop. At each test of line L2:
(Q) is a permutation of ; ; each item of is at most each item of ; and each item of is at most each item of .
Consequence of (Q). Let be the sorted list of the items of . Exactly items lie before the block and each is at most each item of , and the items after the block are at least each item of . So the sorted order of is: a sorted order of , then , then a sorted order of . Hence , the item of rank of the block.
- Initialization. Line L1 sets , and , so and the outer parts are empty.
- Maintenance. Assume (Q) and . Lines L3 and L4 swap two entries of the block. By the lemma, line L5 permutes the block and returns with . If , line L6 returns; this case is treated below. If , line L7 sets . Then . The items now right of the block are of the old block, which are at least , which is at least each item of , and the old part , which (Q) bounds below by each item of the old block. So (Q) holds. If , line L8 sets , and the argument is the mirror one: of the old block is at most each item of .
- Termination. In each pass that does not return, the new range is a part of that does not hold , so drops by at least 1. It starts at , so the loop ends after at most passes.
Conclusion. The run ends in one of two ways.
- Line L6 returns with . Write for the range of this pass. By the lemma, exactly items of the block are at most and lie before it, and the others are greater. So is , and by the consequence of (Q) it is . The items before index are at most by (Q) and the lemma, and the items after it are at least .
- The loop stops with and line L9 returns . By (Q), , so and the block is . By the consequence of (Q), , and (Q) gives the order of the other items.
In both cases the result is , and is a permutation of because each step is a swap. The proof does not use the values of the random indices of line L3, so it holds for every sequence of random choices.