Algorithm Search⌘K

Proofs: Polyphase merge sort

These are the human proofs of Polyphase merge sort, at full length.

Polyphase merge sort sorts in l phases full

This proof shows: correctness.

We use the line numbers of the pseudocode. Let P=T−1≥2P = T - 1 \ge 2. A distribution is a vector of run counts for the PP input tapes. Define the perfect distributions by a(0)=(1,0,…,0)a^{(0)} = (1, 0, \ldots, 0) and

a(ℓ+1)=(a1+a2, a1+a3, …, a1+aP, a1)for a(ℓ)=(a1,…,aP).a^{(\ell+1)} = \big(a_1 + a_2,\ a_1 + a_3,\ \ldots,\ a_1 + a_P,\ a_1\big) \quad\text{for } a^{(\ell)} = (a_1, \ldots, a_P).

Write tℓt_\ell for the sum of the entries of a(ℓ)a^{(\ell)}.

Lemma 1 (shape). For each ℓ≥0\ell \ge 0, a(ℓ)a^{(\ell)} is non-increasing, and for ℓ≥1\ell \ge 1 all its entries are at least 1. The totals grow strictly: tℓ+1>tℓt_{\ell+1} > t_\ell.

Proof. a(0)a^{(0)} is non-increasing. If a1≥⋯≥aP≥0a_1 \ge \cdots \ge a_P \ge 0, then a1+a2≥⋯≥a1+aP≥a1a_1 + a_2 \ge \cdots \ge a_1 + a_P \ge a_1, so a(ℓ+1)a^{(\ell+1)} is non-increasing, and each entry is at least a1≥1a_1 \ge 1. Also tℓ+1=tℓ+(P−1)a1>tℓt_{\ell+1} = t_\ell + (P - 1)a_1 > t_\ell, because P≥2P \ge 2 and a1≥1a_1 \ge 1. ■\blacksquare

So line L2 finds a level ℓ\ell, and tℓ≥St_\ell \ge S. Line L4 gives each tape at most ada_d real runs, because it picks the tape with the largest deficit, and the total deficit tℓ−St_\ell - S before the last real run is at least 1. Line L5 fills each tape to exactly ada_d runs. A dummy run is an empty run.

Lemma 2 (one phase). Suppose that before line L8 the output tape is empty and the PP input tapes hold the counts of a(j)a^{(j)}, j≥1j \ge 1, in some order. Then after line L11 the new output tape is empty and the PP input tapes hold the counts of a(j−1)a^{(j-1)}, in some order.

Proof. Let a(j−1)=(b1,…,bP)a^{(j-1)} = (b_1, \ldots, b_P), so the input tapes hold b1+b2,…,b1+bP,b1b_1 + b_2, \ldots, b_1 + b_P, b_1. The smallest of these is k=b1k = b_1, by Lemma 1. Lines L9 and L10 take b1b_1 runs from each input tape and write b1b_1 runs to the output tape. The input tapes now hold b2,…,bP,0b_2, \ldots, b_P, 0, and the old output tape holds b1b_1. The tape that held b1b_1 runs is empty. If j−1≥1j - 1 \ge 1, all bi≥1b_i \ge 1, so it is the only empty tape, and line L11 picks it. If j−1=0j - 1 = 0, then b=(1,0,…,0)b = (1, 0, \ldots, 0), all input tapes are empty, line L11 picks one of them, and the counts are b1=1b_1 = 1 on the old output tape and 0 elsewhere. In both cases the counts are those of a(j−1)a^{(j-1)}. ■\blacksquare

Lemma 3 (contents). At each test of line L7, each run on a tape is sorted, and the multiset union of all runs on the tapes is the multiset of the items of FF.

Proof. Line L1 makes sorted runs whose union is FF, and dummy runs are empty. Line L10 removes one run from each input tape and appends their merge. The merge of sorted runs is sorted, and it holds exactly the items of the runs it merged. ■\blacksquare

Theorem. If S≥2S \ge 2, the loop of line L7 runs exactly ℓ\ell times, and line L12 returns the items of FF in non-decreasing order.

Proof. Before the first phase, the input tapes hold a(ℓ)a^{(\ell)} and the output tape is empty. By Lemma 2 and induction, after qq phases the tapes hold the counts of a(ℓ−q)a^{(\ell - q)}, with total tℓ−qt_{\ell - q}. By Lemma 1, tℓ−q>1t_{\ell-q} > 1 for q<ℓq < \ell, and t0=1t_0 = 1. So the test of line L7 fails for the first time after ℓ\ell phases, when exactly one run is left. By Lemma 3, this run is sorted and holds all items of FF. ■\blacksquare

The sort is not stable. Take F=(5a,5b,5c)F = (5_a, 5_b, 5_c), three equal keys, with M=1M = 1 and T=3T = 3. The distribution is (2,1)(2, 1): tape 1 holds 5a,5b5_a, 5_b and tape 2 holds 5c5_c. Phase 1 merges 5a5_a and 5c5_c onto tape 3. Phase 2 merges 5b5_b from tape 1 with 5a5c5_a 5_c from tape 3, and the merge takes tape 1 first on equal keys, so the output is 5b,5a,5c5_b, 5_a, 5_c.

Back to Polyphase merge sort