Proofs: Ford-Johnson algorithm
These are the human proofs of Ford-Johnson algorithm, at full length.
Merge insertion returns the input in sorted order, with at most F(n) comparisons full
This proof shows: correctness, termination.
Write and , so the losers are , and has no partner when is odd. Let and for , so , , . A short check gives for , because the terms and cancel.
Termination. By induction on . For , line L1 returns. For , the call of line L5 is on items, so it ends by induction, and the loops of lines L7 to L10 are finite: the values grow, so line L7 stops after the first with , and each binary search ends.
Lemma 1 (the groups cover each loser once). The ranges of line L8, for while , are disjoint and cover .
Proof. The are strictly increasing, so the ranges are disjoint and follow each other without gaps from . The loop stops at the first with , and the last range ends at .
Lemma 2 (the chain stays sorted). After line L6, and after each insertion of line L10, is in non-decreasing order, and for each not yet inserted with , is in .
Proof. After line L5 the pairs are in the order , and each pair still has from line L3, because the recursive call moves pairs as units. So is sorted. The winners are never removed from , so each stays in .
Now let be sorted, and insert . If , let be the position of . Each item with is at least . So there is a place for in , at or before , that keeps sorted: right after the last item of that is not greater than , or at 0 if there is none. The binary search of line L10 over returns this place: it keeps the range of places such that each item before is at most and each item from to is greater than , and the range shrinks to one place. Inserting there keeps sorted. If , the same argument with applies.
Correctness. By induction on , MERGE-INSERTION returns the items of in sorted order. For this is clear. For : by line L6 and Lemma 1, each and each enters exactly once, and nothing else does, so at line L11 holds exactly the items of . By Lemma 2, is sorted. The recursive call of line L5 is correct by induction, because it sorts the pairs by the order of their winners, which is a total preorder, and the claim for the call needs only that the order is total. Equal winners may come out in any order, which does not affect the argument.
Lemma 3 (the comparison bound). When with is inserted, the binary search makes at most comparisons.
Proof. Let first. The part to the left of can only hold items less than or equal to in the sorted chain, and these are among: the winners ; the losers inserted at line L6 or in earlier groups; and the losers of the current group, which line L8 inserts before . The losers with are not yet in . So . If , then is the first index of its group to be inserted, and holds and , so . A binary search over items halves a range of at most places at each comparison, so it ends after at most comparisons.
Adding comparisons for line L3, the comparisons of the recursive call, and Lemma 3 for each loser gives a worst case that satisfies the recurrence of Knuth, Section 5.3.1, whose solution is .