Algorithm Search⌘K

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 s[i]=f[0]+⋯+f[i−1]s[i] = f[0] + \cdots + f[i-1]. If f[i]=f[j]=1f[i] = f[j] = 1 and i<ji < j, then s[j]≥s[i]+f[i]=s[i]+1s[j] \ge s[i] + f[i] = s[i] + 1, so the flagged items get distinct output indices in increasing order of their input index. The largest index used is s[n−1]+f[n−1]−1=total−1s[n-1] + f[n-1] - 1 = total - 1, so the indices are exactly 0,…,total−10, \ldots, total - 1. The writes of line L11 go to distinct cells, so they do not interfere. □\square

Lemma 2. PAR-QUICKSORT returns its input sorted and stably

Define the stable order of the input: uu before vv if uu has a smaller key, or the keys are equal and uu has the smaller input index. The claim is that the output lists the items in this order.

Proof. By induction on nn. For n≤1n \le 1 the claim holds. For n≥2n \ge 2, each key of AA satisfies exactly one of A[i]<xA[i] < x, A[i]=xA[i] = x and A[i]>xA[i] > x, so by Lemma 1, LL, EE and GG split the items of AA, and each part keeps the input order. Every item of LL has a smaller key than every item of EE, which has a smaller key than every item of GG, so in the stable order all of LL comes first, then all of EE, then all of GG. The items of EE have equal keys and are in input order, so EE is in stable order. The parts LL and GG have fewer than nn items, because xx is in EE. The stable order of LL is the stable order of AA restricted to LL, because PACK keeps input order, so by induction L′L' lists LL in stable order, and the same holds for G′G'. So L′∥E∥G′L' \parallel E \parallel G' lists AA in stable order. The two calls of lines L6 and L7 read and write disjoint arrays, so they do not interfere. □\square

Lemma 3. A random pivot is good with probability at least 1/2

Consider a call on s≥2s \ge 2 keys. Call the pivot good if ∣L∣≤3s/4|L| \le 3s/4 and ∣G∣≤3s/4|G| \le 3s/4.

Proof. Sort the ss items of the call by the stable order, and let uu be the position of the pivot item in this list, from 00 to s−1s - 1. Since ii is uniform, uu is uniform. The items of LL come before the pivot item, so ∣L∣≤u|L| \le u. The items of GG come after it, so ∣G∣≤s−1−u|G| \le s - 1 - u. So the pivot is good when u≤3s/4u \le 3s/4 and u≥s/4−1u \ge s/4 - 1. The integers uu in the interval [s/4−1,3s/4][s/4 - 1, 3s/4], which has length s/2+1s/2 + 1, number at least s/2s/2, and they all lie in [0,s−1][0, s - 1]. So the probability is at least 1/21/2. □\square

Lemma 4. The recursion depth is O(log n) with high probability

Let n≥2n \ge 2, k=⌈log⁡4/3n⌉+1k = \lceil \log_{4/3} n \rceil + 1 and D=8kD = 8k. With probability at least 1−n−2.91 - n^{-2.9}, no call is at depth more than DD.

Proof. Fix one item yy of the input. Consider the chain of calls whose input contains yy: the top call, then the call on LL or GG that contains yy, and so on, until a call with yy in EE or a call on one item. If a call of this chain has ss items and a good pivot, the next call of the chain has at most 3s/43s/4 items. So after kk good pivots the size is at most (3/4)kn≤3/4<1(3/4)^k n \le 3/4 < 1, which is impossible, so the chain ends before the call after its kk-th good pivot. In other words, a chain with DD calls of size at least 2 has fewer than kk good pivots among its first DD calls.

The pivot of each call is drawn afresh, and by Lemma 3 it is good with probability at least 1/21/2 whatever happened before. So the number of good pivots among the first DD calls of the chain, if the chain is that long, is at least a binomial variable XX with DD trials and success probability 1/21/2, in the sense of stochastic domination. Its mean is μ=D/2=4k\mu = D/2 = 4k. By the Chernoff bound Pr⁡[X<(1−δ)μ]≤e−δ2μ/2\Pr[X < (1 - \delta)\mu] \le e^{-\delta^2 \mu / 2} with δ=3/4\delta = 3/4,

Pr⁡[X<k]≤e−(9/16)(4k)/2=e−9k/8.\Pr[X < k] \le e^{-(9/16)(4k)/2} = e^{-9k/8}.

Since k≥log⁡4/3n=ln⁡n/ln⁡(4/3)≥3.47ln⁡nk \ge \log_{4/3} n = \ln n / \ln(4/3) \ge 3.47 \ln n, this is at most e−3.9ln⁡n=n−3.9e^{-3.9 \ln n} = n^{-3.9}. Every call at depth more than DD lies on the chain of some item, so a union bound over the nn items gives a failure probability of at most n⋅n−3.9=n−2.9n \cdot n^{-3.9} = n^{-2.9}. □\square

Theorem. Span and work

In the work-span model, PAR-QUICKSORT on nn keys has span O(log⁡2n)O(\log^2 n) with probability at least 1−n−2.91 - n^{-2.9}, and expected work O(nlog⁡n)O(n \log n).

Proof. A call on ss keys does lines L1 to L5 and L9 with work O(s)O(s) and span O(log⁡s)O(\log s): 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 O(log⁡n)O(\log n), which is O(log⁡2n)O(\log^2 n) when the event of Lemma 4 holds.

For the work, charge the O(s)O(s) work of a call to its ss items, O(1)O(1) each. An item is charged once for each call on its chain. The chain of an item ends after at most kk good pivots, and each call has a good pivot with probability at least 1/21/2 whatever happened before, so the expected number of calls on the chain is at most 2k2k. By linearity of expectation the expected work is O(nk)=O(nlog⁡n)O(nk) = O(n \log n). □\square

Back to Parallel quicksort