Algorithm Search⌘K

Proofs: Cole's parallel merge sort

These are the human proofs of Cole's parallel merge sort, at full length.

Cole's merge sort sorts in 3 lg n stages full

This proof shows: correctness.

Let n=2kn = 2^k and assume the keys are distinct (ties are broken by input index). For a node vv of height hh, L(v)L(v) is the sorted list of the 2h2^h keys at the leaves below vv. Write UPt(v)\mathrm{UP}_t(v) and SUPt(v)\mathrm{SUP}_t(v) for the lists after stage tt, with UP0(v)=[key]\mathrm{UP}_0(v) = [\text{key}] at a leaf and UP0(v)=∅\mathrm{UP}_0(v) = \emptyset at an inner node. A node is full at stage tt if UPt(v)=L(v)\mathrm{UP}_t(v) = L(v).

Lemma 1. The lists are sorted subsets

For every tt and vv, SUPt(v)\mathrm{SUP}_t(v) and UPt(v)\mathrm{UP}_t(v) are sorted lists of distinct items of L(v)L(v), provided that each stage computes NEWUP(u)\mathrm{NEWUP}(u) as the sorted union of SUPt(x)\mathrm{SUP}_t(x) and SUPt(y)\mathrm{SUP}_t(y).

Proof. By induction on tt. A sample of a sorted list of items of L(v)L(v) is again one. The union of a list of items of L(x)L(x) and one of items of L(y)L(y) has distinct items, because L(x)L(x) and L(y)L(y) are disjoint, and its items are in L(u)L(u). □\square

Lemma 2. Line L7 finds the rank

Let e∈SUP(x)e \in \mathrm{SUP}(x), let rr be the number of items of UP(u)\mathrm{UP}(u) less than ee, and let d=UP(u)[r−1]d = \mathrm{UP}(u)[r-1] and f=UP(u)[r]f = \mathrm{UP}(u)[r], with d=−∞d = -\infty if r=0r = 0 and f=+∞f = +\infty if r=∣UP(u)∣r = |\mathrm{UP}(u)|. Let ℓ\ell be the number of items of SUP(y)\mathrm{SUP}(y) less than dd. Then the rank of ee in SUP(y)\mathrm{SUP}(y) is ℓ\ell plus the number of items of SUP(y)\mathrm{SUP}(y) in [d,f)[d, f) that are less than ee.

Proof. We have d<e≤fd < e \le f. Items of SUP(y)\mathrm{SUP}(y) less than dd are less than ee. Items of SUP(y)\mathrm{SUP}(y) that are at least ff are at least ee, and none of them equals ee, because ee is below xx and they are below yy. So the items of SUP(y)\mathrm{SUP}(y) less than ee are the ℓ\ell items below dd and those in [d,f)[d, f) below ee. □\square

The lemma holds for any lists. The 3-cover property only bounds the number of items in [d,f)[d, f), which matters for the time and not for the result.

Lemma 3. The merge is correct

Line L8 writes each item of SUP(x)∪SUP(y)\mathrm{SUP}(x) \cup \mathrm{SUP}(y) to a distinct position, and the result is the sorted union.

Proof. For e∈SUP(x)e \in \mathrm{SUP}(x), the items of the union less than ee are the items of SUP(x)\mathrm{SUP}(x) less than ee and those of SUP(y)\mathrm{SUP}(y) less than ee. Their number is the sum that line L8 uses, by Lemma 2, and the same holds for items of SUP(y)\mathrm{SUP}(y). In a set of distinct items, the number of smaller items is the position in sorted order. □\square

Lemma 4. Size before a node is full

For a node vv of height h≥1h \ge 1 and 0≤t≤3h0 \le t \le 3h, ∣UPt(v)∣≤2h⋅8t/3−h|\mathrm{UP}_t(v)| \le 2^h \cdot 8^{t/3 - h}. In particular ∣UPt(v)∣<2h|\mathrm{UP}_t(v)| < 2^h for t<3ht < 3h, so vv is not full before stage 3h3h.

Proof. By induction on hh, with the claim for height 00 read as: a leaf is full from stage 00 on. Let xx and yy be the children of vv, of height h−1h - 1. Then ∣UPt(v)∣≤∣SUPt(x)∣+∣SUPt(y)∣|\mathrm{UP}_t(v)| \le |\mathrm{SUP}_t(x)| + |\mathrm{SUP}_t(y)|. We prove Lemmas 4 and 5 together by induction on hh, so we may use that xx and yy become full at exactly stage 3h−33h - 3. For t=0t = 0 the list is empty, so let t≥1t \ge 1.

Case t≤3h−3t \le 3h - 3. The children are not full at stage t−1<3h−3t - 1 < 3h - 3, so their samples are every fourth item, and ∣SUPt(x)∣≤∣UPt−1(x)∣/4≤2h−1⋅8(t−1)/3−(h−1)/4|\mathrm{SUP}_t(x)| \le |\mathrm{UP}_{t-1}(x)|/4 \le 2^{h-1} \cdot 8^{(t-1)/3 - (h-1)}/4. Since 8−1/3⋅8/4=18^{-1/3} \cdot 8 / 4 = 1, twice this is 2h⋅8t/3−h2^h \cdot 8^{t/3 - h}.

Case t=3h−3+at = 3h - 3 + a with a∈{1,2,3}a \in \{1, 2, 3\}. The children became full at stage 3h−33h - 3, so their samples have 2h−1/42^{h-1}/4, 2h−1/22^{h-1}/2 and 2h−12^{h-1} items for a=1,2,3a = 1, 2, 3, at most, which is 2h−1⋅2a−32^{h-1} \cdot 2^{a - 3}. Twice this is 2h⋅2t−3h=2h⋅8t/3−h2^h \cdot 2^{t - 3h} = 2^h \cdot 8^{t/3 - h}. □\square

Lemma 5. A node of height h is full at stage 3h

Proof. By induction on hh. Leaves are full at stage 00. Let vv have height h≥1h \ge 1. Its children are full at stage 3h−33h - 3, so at stage 3h3h their samples are all of L(x)L(x) and all of L(y)L(y), and by Lemma 3, UP3h(v)=L(x)∪L(y)\mathrm{UP}_{3h}(v) = L(x) \cup L(y) in sorted order, which is L(v)L(v). By Lemma 4 it is not full earlier. After stage 3h3h the node keeps its list, and it sends samples only in stages 3h+13h + 1 to 3h+33h + 3. □\square

Theorem

After stage 3k3k, UP(root)\mathrm{UP}(\text{root}) is L(root)L(\text{root}), the input keys in sorted order.

Proof. The root has height kk, so this is Lemma 5 for h=kh = k. With ties broken by input index, equal keys keep their input order, and removing the padding keys, which are larger than all real keys and sit at the end, gives the sorted input. □\square

Back to Cole's parallel merge sort