Proofs: Replacement selection
These are the human proofs of Replacement selection, at full length.
Replacement selection makes sorted runs, each but the last with at least P items full
This proof shows: correctness.
We use the line numbers of the pseudocode, and we write for the number of the current run. When , let be the last item written by line L7. All items of are distinct as triples, because their positions are distinct.
Invariant. At each test of line L4:
- Each run of is sorted, and no run is empty.
- If , each item in has run number 1. If , each item in has run number or , and each item with run number has a key of at least .
- The items written to , the items in and the items not yet read are together the items of , each one time.
Initialization. Line L2 gives each item in the run number 1, and is empty. So the invariant is true.
Maintenance. Let be the minimum of at line L5.
Case . Then , line L6 appends run 1, and line L7 makes it , which is sorted.
Case and . By part 2, , so run stays sorted when line L7 appends . Line L6 does nothing, because .
Case and . Because the heap orders by run number first, no item in has run number . Line L6 appends run , and line L7 makes it . The current run is now .
In each case, the new current run is and the new last item is . We check part 2 for the new values. Each other item in has run number or : in the first two cases because the numbers in were or with , and in the third case because they were all . Each other item in with run number is at least , because is the minimum among the items with run number . If line L8 reads an item , line L9 gives it the number only when , and else . So part 2 stays true. Line L10 or line L11 removes from , and is now in , so part 3 stays true. Part 1 holds by the three cases.
Termination. Each iteration writes exactly one item to . By part 3, at most items can be written. So the loop stops after at most iterations. It stops only when is empty. Line L11 runs only when no item is left to read, so at that point all items are written, and the loop ran exactly times.
Theorem 1. REPLACEMENT-SELECTION returns sorted, non-empty runs whose items together are the items of , each one time.
Proof. At the end, is empty and no item is left to read. Parts 1 and 3 of the invariant give the claim.
Theorem 2. Each run except the last has at least items.
Proof. Take a run that is not the last run. Run starts at a step where all items in have run number . First let . Run started at a step where all items in had run number , and all of them are written to run . If the input was not empty at that step, line L10 kept the heap at items since line L2, so the heap held items, and run has at least items. If the input was empty at that step, then no item is read after it, so no item gets the run number , and run is the last run. This is false. Now let . When run 2 starts, holds at least one item with run number 2. Such an item was read by line L8 at a step that wrote an item to run 1. If the heap held fewer than items after line L2, the input was empty, no item was read, and there is no run 2. So the heap held items, all of them with run number 1, and they are all written to run 1.
Theorem 3 (equal keys). Let and be items with equal keys, where comes before in . Then the run of is at most the run of , and if they are in the same run, comes first in it.
Proof. Write for the run number of item . An item that line L2 reads gets , and an item that line L8 reads at a step with current run gets or . The current run never decreases. If is read by line L2, then . Else let and be the current runs at the steps that read and , with . If , then . If and , then the key of is less than the item just written at that step. The items written to run are written in sorted order by Theorem 1, so the item written at the step that reads satisfies . The key of equals the key of , so it is less than , and . In all cases . If , the heap order by run number, then key, then position puts before , because has the smaller position, and both are in the heap or was already written when was read.