Algorithm Search⌘K

Proofs: Balanced multiway merge sort

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

Balanced merge sort sorts stably in ceil(log_P S) passes full

This proof shows: correctness.

We use the line numbers of the pseudocode. Let P=⌊T/2⌋≥2P = \lfloor T/2 \rfloor \ge 2. Let R0,R1,…,RS−1R_0, R_1, \ldots, R_{S-1} be the runs made by line L4, in this order, and let FjF_j be the part of FF that run RjR_j came from. So F=F0F1⋯FS−1F = F_0 F_1 \cdots F_{S-1} and RjR_j is the stable sort of FjF_j. For a sequence XX, write st⁡(X)\operatorname{st}(X) for its stable sort.

Lemma (merge). If X1,…,XgX_1, \ldots, X_g are sequences and the merge of st⁡(X1),…,st⁡(Xg)\operatorname{st}(X_1), \ldots, \operatorname{st}(X_g) takes the run with the smaller index on equal keys, the result is st⁡(X1⋯Xg)\operatorname{st}(X_1 \cdots X_g).

This is Lemma 1 of the proof page of external merge sort; the same proof applies.

Invariant. At each test of line L5 there are a number s≥0s \ge 0 and consecutive parts G0,…,Gs−1G_0, \ldots, G_{s-1} of FF, with F=G0⋯Gs−1F = G_0 \cdots G_{s-1}, such that for each c<sc < s the run st⁡(Gc)\operatorname{st}(G_c) is the run at index ⌊c/P⌋\lfloor c/P \rfloor of tape Ac mod PA_{c \bmod P}, and the tapes AA hold no other runs.

Initialization. Line L4 writes run RjR_j to tape Aj mod PA_{j \bmod P}, and it is the ⌊j/P⌋\lfloor j/P \rfloor-th run written to that tape, because the runs j′<jj' < j with j′≡j(modP)j' \equiv j \pmod P are exactly ⌊j/P⌋\lfloor j/P \rfloor in number. So the invariant holds with s=Ss = S and Gc=FcG_c = F_c.

Maintenance. In iteration ii of line L7, the tapes AdA_d that still have a run at index ii are those with iP+d<siP + d < s, so line L8 merges the runs st⁡(Gc)\operatorname{st}(G_c) for iP≤c<min⁡(iP+P,s)iP \le c < \min(iP + P, s), in the order of dd, which is the order of cc. By the lemma, the result is st⁡(Gi′)\operatorname{st}(G'_i) with Gi′=GiP⋯Gmin⁡(iP+P,s)−1G'_i = G_{iP} \cdots G_{\min(iP+P, s) - 1}. The loop of line L7 runs for i=0,…,⌈s/P⌉−1i = 0, \ldots, \lceil s/P \rceil - 1. Line L8 writes st⁡(Gi′)\operatorname{st}(G'_i) to Bi mod PB_{i \bmod P}, and it is the ⌊i/P⌋\lfloor i/P \rfloor-th run written to that tape, by the same count as in the initialization. The parts Gi′G'_i are consecutive and their concatenation is FF. After line L9 renames BB to AA, the invariant holds with s′=⌈s/P⌉s' = \lceil s/P \rceil and the parts Gi′G'_i.

Termination and the pass count. The loop of line L5 runs while s≥2s \ge 2, and a pass maps ss to ⌈s/P⌉\lceil s/P \rceil. For s≥2s \ge 2 and P≥2P \ge 2, ⌈s/P⌉≤⌈s/2⌉≤s−1\lceil s/P \rceil \le \lceil s/2 \rceil \le s - 1, so the loop stops. For integers x≥1x \ge 1, ⌈⌈x/Pq⌉/P⌉=⌈x/Pq+1⌉\lceil \lceil x/P^q \rceil / P \rceil = \lceil x/P^{q+1} \rceil, so after qq passes s=⌈S/Pq⌉s = \lceil S/P^q \rceil. This is 1 exactly when Pq≥SP^q \ge S. So for S≥1S \ge 1 the number of passes is ⌈log⁡PS⌉\lceil \log_P S \rceil.

Theorem. BALANCED-MERGE-SORT returns st⁡(F)\operatorname{st}(F) on tape A0A_0.

Proof. If N=0N = 0, there are no runs, and the result is empty. Else the loop stops with s=1s = 1, and by the invariant the only run is st⁡(G0)\operatorname{st}(G_0) with G0=FG_0 = F, at index 0 of tape A0A_{0}. ■\blacksquare

Back to Balanced multiway merge sort