Proofs: Parallel quicksort
These are the human proofs of Parallel quicksort, at full length.
Parallel quicksort is correct and stable, with span O(log² n) with high probability full
This proof shows: correctness.
Lemma 1. PACK returns the flagged items in input order
Proof. Let . If and , then , so the flagged items get distinct output indices in increasing order of their input index. The largest index used is , so the indices are exactly . The writes of line L11 go to distinct cells, so they do not interfere.
Lemma 2. PAR-QUICKSORT returns its input sorted and stably
Define the stable order of the input: before if has a smaller key, or the keys are equal and has the smaller input index. The claim is that the output lists the items in this order.
Proof. By induction on . For the claim holds. For , each key of satisfies exactly one of , and , so by Lemma 1, , and split the items of , and each part keeps the input order. Every item of has a smaller key than every item of , which has a smaller key than every item of , so in the stable order all of comes first, then all of , then all of . The items of have equal keys and are in input order, so is in stable order. The parts and have fewer than items, because is in . The stable order of is the stable order of restricted to , because PACK keeps input order, so by induction lists in stable order, and the same holds for . So lists in stable order. The two calls of lines L6 and L7 read and write disjoint arrays, so they do not interfere.
Lemma 3. A random pivot is good with probability at least 1/2
Consider a call on keys. Call the pivot good if and .
Proof. Sort the items of the call by the stable order, and let be the position of the pivot item in this list, from to . Since is uniform, is uniform. The items of come before the pivot item, so . The items of come after it, so . So the pivot is good when and . The integers in the interval , which has length , number at least , and they all lie in . So the probability is at least .
Lemma 4. The recursion depth is O(log n) with high probability
Let , and . With probability at least , no call is at depth more than .
Proof. Fix one item of the input. Consider the chain of calls whose input contains : the top call, then the call on or that contains , and so on, until a call with in or a call on one item. If a call of this chain has items and a good pivot, the next call of the chain has at most items. So after good pivots the size is at most , which is impossible, so the chain ends before the call after its -th good pivot. In other words, a chain with calls of size at least 2 has fewer than good pivots among its first calls.
The pivot of each call is drawn afresh, and by Lemma 3 it is good with probability at least whatever happened before. So the number of good pivots among the first calls of the chain, if the chain is that long, is at least a binomial variable with trials and success probability , in the sense of stochastic domination. Its mean is . By the Chernoff bound with ,
Since , this is at most . Every call at depth more than lies on the chain of some item, so a union bound over the items gives a failure probability of at most .
Theorem. Span and work
In the work-span model, PAR-QUICKSORT on keys has span with probability at least , and expected work .
Proof. A call on keys does lines L1 to L5 and L9 with work and span : line L4 is a parallel loop, and each PACK is a prefix sum and a parallel loop. The two recursive calls run in parallel. So the span is at most the recursion depth times , which is when the event of Lemma 4 holds.
For the work, charge the work of a call to its items, each. An item is charged once for each call on its chain. The chain of an item ends after at most good pivots, and each call has a good pivot with probability at least whatever happened before, so the expected number of calls on the chain is at most . By linearity of expectation the expected work is .