Algorithm Search⌘K

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 m=⌊n/2⌋m = \lfloor n/2 \rfloor and h=⌈n/2⌉h = \lceil n/2 \rceil, so the losers are b1,…,bhb_1, \ldots, b_h, and bhb_h has no partner when nn is odd. Let t1=1t_1 = 1 and tk=(2k+1+(−1)k)/3t_k = (2^{k+1} + (-1)^k)/3 for k≥2k \ge 2, so t2=3t_2 = 3, t3=5t_3 = 5, t4=11t_4 = 11. A short check gives tk+tk−1=(2k+1+2k)/3=2kt_k + t_{k-1} = (2^{k+1} + 2^k)/3 = 2^k for k≥2k \ge 2, because the terms (−1)k(-1)^k and (−1)k−1(-1)^{k-1} cancel.

Termination. By induction on nn. For n≤1n \le 1, line L1 returns. For n≥2n \ge 2, the call of line L5 is on m<nm < n items, so it ends by induction, and the loops of lines L7 to L10 are finite: the values tkt_k grow, so line L7 stops after the first kk with tk≥ht_k \ge h, and each binary search ends.

Lemma 1 (the groups cover each loser once). The ranges tk−1+1,…,min⁡(tk,h)t_{k-1} + 1, \ldots, \min(t_k, h) of line L8, for k=2,3,…k = 2, 3, \ldots while tk−1<ht_{k-1} < h, are disjoint and cover 2,…,h2, \ldots, h.

Proof. The tkt_k are strictly increasing, so the ranges are disjoint and follow each other without gaps from t1+1=2t_1 + 1 = 2. The loop stops at the first kk with tk≥ht_k \ge h, and the last range ends at min⁡(tk,h)=h\min(t_k, h) = h. □\square

Lemma 2 (the chain stays sorted). After line L6, and after each insertion of line L10, SS is in non-decreasing order, and for each jj not yet inserted with j≤mj \le m, aja_j is in SS.

Proof. After line L5 the pairs are in the order a1≤⋯≤ama_1 \le \cdots \le a_m, and each pair still has bi≤aib_i \le a_i from line L3, because the recursive call moves pairs as units. So (b1,a1,…,am)(b_1, a_1, \ldots, a_m) is sorted. The winners are never removed from SS, so each aja_j stays in SS.

Now let SS be sorted, and insert bjb_j. If j≤mj \le m, let uu be the position of aja_j. Each item S[i]S[i] with i≥ui \ge u is at least S[u]=aj≥bjS[u] = a_j \ge b_j. So there is a place for bjb_j in S[0…u]S[0 \ldots u], at or before uu, that keeps SS sorted: right after the last item of S[0…u−1]S[0 \ldots u-1] that is not greater than bjb_j, or at 0 if there is none. The binary search of line L10 over S[0…u−1]S[0 \ldots u-1] returns this place: it keeps the range of places [lo,hi][lo, hi] such that each item before lolo is at most bjb_j and each item from hihi to u−1u-1 is greater than bjb_j, and the range shrinks to one place. Inserting bjb_j there keeps SS sorted. If j>mj > m, the same argument with u=∣S∣u = |S| applies. □\square

Correctness. By induction on nn, MERGE-INSERTION returns the items of XX in sorted order. For n≤1n \le 1 this is clear. For n≥2n \ge 2: by line L6 and Lemma 1, each aia_i and each bib_i enters SS exactly once, and nothing else does, so at line L11 SS holds exactly the items of XX. By Lemma 2, SS 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 bjb_j with tk−1<j≤tkt_{k-1} < j \le t_k is inserted, the binary search makes at most kk comparisons.

Proof. Let j≤mj \le m first. The part S[0…u−1]S[0 \ldots u-1] to the left of aja_j can only hold items less than or equal to aja_j in the sorted chain, and these are among: the winners a1,…,aj−1a_1, \ldots, a_{j-1}; the losers b1,…,btk−1b_1, \ldots, b_{t_{k-1}} inserted at line L6 or in earlier groups; and the losers bj+1,…,bmin⁡(tk,h)b_{j+1}, \ldots, b_{\min(t_k, h)} of the current group, which line L8 inserts before bjb_j. The losers bj′b_{j'} with j′>tkj' > t_k are not yet in SS. So u≤(j−1)+tk−1+(tk−j)=2k−1u \le (j - 1) + t_{k-1} + (t_k - j) = 2^k - 1. If j=h>mj = h > m, then jj is the first index of its group to be inserted, and SS holds a1,…,ama_1, \ldots, a_m and b1,…,btk−1b_1, \ldots, b_{t_{k-1}}, so ∣S∣=(j−1)+tk−1≤2k−1|S| = (j - 1) + t_{k-1} \le 2^k - 1. A binary search over u≤2k−1u \le 2^k - 1 items halves a range of at most 2k2^k places at each comparison, so it ends after at most kk comparisons. □\square

Adding mm 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 F(n)=∑k=1n⌈lg⁡(3k/4)⌉F(n) = \sum_{k=1}^{n} \lceil \lg (3k/4) \rceil. ■\blacksquare

Back to Ford-Johnson algorithm