Proofs: Sink (sift-down) operation
These are the human proofs of Sink (sift-down) operation, at full length.
Sink restores a max-heap, and heapsort with sink sorts full
This proof shows: correctness, termination.
Positions run from 1 to . The children of are and when they are at most . Let be the set of positions of the subtree of . A pair with a child of is in order when , that is, the item at is not smaller than the item at . The relation less is a strict weak order on the items, so "not smaller" is transitive.
Lemma (SINK). Let be a position such that each pair of whose parent is not is in order. Then SINK ends after at most tests of line L1. At the end, each pair of is in order, the items of are a permutation of the old ones, and no other position changes.
Proof. We show this invariant at each test of line L1:
(I) . (S1) Each pair of whose parent is not is in order. (S2) If and is the parent of , the item at is not smaller than the item at any child of . Only positions of have changed, by exchanges.
Initialization. At the first test, , (S1) is the hypothesis, and (S2) does not apply.
Maintenance. Assume (I) and . After line L3, is a child of whose item is not smaller than the item of the other child, if any. If line L4 breaks, we treat it below. Else : write for the item at and for the item at , so . After line L5, is at and is at .
- The pair is in order, since .
- The pair for the other child is in order, by the choice of .
- If , the pair with the parent of is in order, since by (S2) the item at is not smaller than .
- The pairs for the children of are the ones that (S1) for the new leaves out.
So (S1) holds for . For (S2): before the exchange, each pair was in order by (S1), since , so is not smaller than the item at , and is now at the parent of . Line L6 sets , and (I) holds again.
Termination. Each pass that does not break replaces by a child, so , the number of levels below , drops by exactly 1, and the loop test fails when no level is left. If the test of line L1 fails, has no child and (S1) covers all pairs. If line L4 breaks, the item at is not smaller than the item at , which is not smaller than the item at the other child, so the pairs from are in order, and with (S1) all pairs of are in order.
Lemma (DEL-MAX). If is a max-heap with , DEL-MAX returns a largest item and leaves a max-heap of the other items. Proof. The root is a largest item, because each item is joined to the root by a chain of pairs in order. After line L8, position 1 holds the old last item and the heap has positions. Each pair whose parent is not 1 joins two positions that did not change, so the lemma on SINK with applies.
Theorem (HEAPSORT). HEAPSORT ends, and then is a permutation of its input in non-decreasing order.
Proof. Construction. We claim that after the call of line L12 for , the subtree of each position is a max-heap. Before the first call, each position has no child, so its subtree is a max-heap. At the call for , the children and are larger than , so their subtrees are max-heaps, and the lemma on SINK applies. The call changes only positions of , so each subtree with is inside , where all pairs are now in order, or disjoint from it, where nothing changed. After the call for , is a max-heap.
Sortdown. Let be the input size and the current heap size. We show this invariant at each test of line L13:
(H) is a max-heap, is in non-decreasing order, and no item of is larger than an item of .
At the first test, and the tail is empty, so (H) follows from the construction. In a pass with , line L14 moves the root, a largest item of the heap, to position , and sets the heap size to . By (H), this item is not larger than the items of the old tail, and it is not smaller than any item that stays in the heap. So the new tail is sorted, and the order between heap and tail holds. Each pair of whose parent is not 1 is unchanged, so the lemma on SINK with makes a max-heap. Thus (H) holds with .
Termination. Each pass lowers by exactly 1, so the loop runs times and stops with . Then has at most one item, which is not larger than the items of the sorted tail, so is sorted. Exchanges keep the multiset of the items.