Proofs: Parallel merge sort
These are the human proofs of Parallel merge sort, at full length.
Parallel merge sort is correct, stable, and has work Θ(n log n) and span O(log³ n) full
This proof shows: correctness.
Fix the inputs of a call of P-MERGE: sorted ranges and , with items. Define the stable order on these items: comes before if has a smaller key, or the keys are equal and is in and is in , or the keys are equal, both are in the same input and has the smaller index. This is a strict total order. The stable merge of the two ranges is the list of the items in this order.
Lemma 1. P-MERGE writes the stable merge to Z[c .. c + N − 1]
Proof. By induction on . For the call writes nothing, which is correct.
Let and , so . Let . Line L12 finds the least with or . Since is sorted, are the items of with key less than , and are those with key at least . The items that come before in the stable order are:
- the items , which have keys at most and smaller indices;
- the items of with key less than , which are . An item of with key equal to comes after , because is in .
No other item comes before : the items have keys at least and larger indices, and the items have keys at least . So exactly items come before , and line L13 gives the position plus this number, which is its position in the stable merge.
The items before are the inputs of the call on line L15, and the items after are the inputs of the call on line L16. The stable order restricted to a subset is the stable order of that subset, because the order depends only on keys, inputs and indices. Each recursive call has fewer than items, so by induction the call on line L15 writes the items before in stable order to , and the call on line L16 writes the items after in stable order to . Together with , this is the stable merge.
The case is the same with . Line L20 finds the least with or , so are the items of with key at most . These come before , because an item of with key equal to comes before . The items also come before , and no other items do. So goes to position , which is line L21, and the two recursive calls handle the rest as before.
The two recursive calls of one call write the disjoint ranges and and only read and . When is not or , the parallel calls do not interfere, and every schedule gives the same result.
Lemma 2. P-MERGE-SORT sorts A[lo .. hi − 1] stably
Proof. By induction on . A range of at most one item is sorted. Otherwise, by induction, lines L3 to L5 sort the two halves stably. These calls work on disjoint ranges of and of , so they do not interfere. Line L6 copies the range to , and line L7 merges and into . The items of the left half have smaller input positions than the items of the right half, so by Lemma 1 the merge puts equal keys in input order. Within each half, equal keys are already in input order. So the result is sorted, and equal keys keep their input order.
Lemma 3. Split sizes
Let a call of P-MERGE have items, and let its two recursive calls have and items. Then , and .
Proof. Take the case , so . The left call gets items of and items of , so . The right call gets items of and at most items of , so as well. Also and , so both are at least . The other case is the same with .
Lemma 4. P-MERGE has span O(log² N) and work Θ(N)
Charge a call with items a cost of at most for itself, for a constant . This covers lines L8 to L14, since a binary search on at most items makes at most probes.
Span. The span satisfies and by Lemma 3. Along any chain of calls the sizes drop by a factor of at least at each step, so a chain has at most calls, each of cost at most . So .
Work. Let . We show for a constant . Since , for each of the finitely many the bound holds when is large enough, so fix such a , with also . Let , and assume the bound for all smaller sizes. By Lemma 3,
Here , and . So it is enough to show , that is . Since , it is enough that , which holds. So . Each of the items is written by its own call, so .
Theorem. Work and span of P-MERGE-SORT
P-MERGE-SORT on keys has work and span in the work-span model.
Proof. The copy on line L6 is a parallel loop over items, with work and span . By Lemma 4 the merge on line L7 has work and span . The two recursive calls run in parallel, so
The first recurrence is the one of sequential merge sort, with solution . The second has levels, each of span , so .