Proofs: Heap-based selection
These are the human proofs of Heap-based selection, at full length.
Heap-based selection returns the k smallest items in sorted order full
This proof shows: correctness, termination.
Let the input be , let , and let be the input in sorted order. The claim is: HEAP-SELECT stops and returns the list .
A list is a max-heap when for each , that is, each item is at least each of its children. A max-heap of size has its largest item at index 0, since each index is joined to 0 by a chain of parents along which the items do not decrease.
Lemma 1 (SIFT-DOWN). Let , and assume that the parent condition holds for each in the subtrees of the children of , that is, these two subtrees are max-heaps. Then SIFT-DOWN stops, permutes by swaps inside the subtree of , and after it the subtree of is a max-heap.
Proof. By induction on the height of in the tree. If has no child, line L13 returns and the subtree is the single item . Else line L14 picks as a child of with the larger item, so is the largest item of the two children, and by the heap order of the child subtrees it is the largest item of both subtrees. If , line L15 returns, and is at least each item below it, so the subtree is a max-heap. Else line L16 swaps and . Now is the largest item of the subtree, at least both children; the other child's subtree did not change; and the subtree of has the max-heap order below its children, so the loop continues at , which has a smaller height. By the induction hypothesis the subtree of is a max-heap at the end, and its items are the same as before as a multiset, so each of them is at most the new . Each pass moves down one level, so the loop stops.
Lemma 2 (build). After line L2, is a max-heap and holds .
Proof. The indices have no child. Line L2 visits . Before the visit of , the subtrees of its children are max-heaps: their roots have larger indices than , so they were visited before, or they are leaves, and later visits of larger indices do not touch them again. By Lemma 1, after the visit the subtree of is a max-heap. After the visit of the whole heap is a max-heap. Only swaps are made, so the items are those of line L1.
Invariant of the loop of line L3. Write for the prefix of the first items read. At each test of line L3, when items have been read:
(S) is a max-heap, and the multiset of its items equals the multiset of the smallest entries of a sorted order of .
- Initialization. If , and the heap is empty; if , and there is no later item. In the main case , and by Lemma 2 the heap holds all of , which are its smallest entries.
- Maintenance. Let be the next item, and let , the largest candidate. Let be sorted, so holds and .
- If , line L4 does nothing. A sorted order of is followed by the other items and in sorted order, since all of these are at least . Its first entries are the items of , so (S) holds.
- If , lines L5 and L6 replace by . A sorted order of is obtained by inserting into at a place before . Its first entries are and , which is the new multiset of . The children of the root had max-heap subtrees before, and they did not change, so by Lemma 1 the heap is a max-heap after line L6.
- Termination of the loop. Each pass reads one item, and there are of them.
At the end of the loop , so by (S) the heap holds as a multiset.
The sort phase. At each test of line L7 with index , is a max-heap and holds the largest items of the heap in increasing order, each at least each item of . At the start and the second part is empty. Line L8 moves the largest item of the first part to index , which keeps the order of the second part, and line L9 restores the heap order of by Lemma 1, since the subtrees of the children of the root did not change. The loop runs times, and at the end is the smallest item and is increasing. So in this order, which line L10 returns.