Proofs: Cascade merge sort
These are the human proofs of Cascade merge sort, at full length.
Cascade merge sort sorts in l passes full
This proof shows: correctness.
We use the line numbers of the pseudocode. Let . The cascade distributions are and, for ,
Let be the sum of the entries of .
Lemma 1 (shape). . For , the entries of are positive and strictly decreasing. For all , .
Proof. The first claim is the rule applied to . If all entries of are positive, the entries of are sums of leading entries, so they are positive and strictly decreasing. Also , since and .
Lines L2 to L4 put runs on tape , real or dummy, as on the proof page of the polyphase merge sort.
Lemma 2 (one pass). Suppose that at line L7 the output tape is empty and the other tapes hold the counts of , , in some order. Then after the loop of line L8, one tape is empty and the other tapes hold the counts of , in some order, and the next pass uses the empty tape as its output.
Proof. Let . The input tapes hold for . Call the input tape with runs tape .
If , all . Line L10 merges once, all input tapes become empty, and the old output holds 1 run. All other tapes are empty, so the counts are those of up to order. The loop of line L8 stops because no tape is active.
If , the are positive by Lemma 1. We show by induction on : before step of the loop of line L8, the active tapes are tapes and tape holds runs. The smallest count is that of tape , namely , and it is the only one equal to , because are positive. Line L10 merges times onto the current output, and tape alone becomes empty. Line L12 removes it from the active tapes and makes it the next output. This proves the claim for .
After step , only tape 1 is active, with runs, and the loop stops. The outputs of the steps received runs. So the tapes hold , and the last tape that became empty, tape 2, is empty and is the output of the next pass.
Lemma 3 (contents). At each test of line L6, each run on a tape is sorted, and the multiset union of the runs is the multiset of the items of .
Proof. Line L1 makes sorted runs whose union is , and dummy runs are empty. Line L11 replaces some runs by their merge, which is sorted and has the same items.
Theorem. If , the loop of line L6 runs exactly times, and line L14 returns the items of in non-decreasing order.
Proof. By Lemma 2 and induction, after passes the tapes hold the counts of , with one tape empty. By Lemma 1, for . So the loop runs exactly times and ends with one run, which by Lemma 3 is sorted and holds all items of .