Algorithm Search⌘K

Proofs: Sample sort

These are the human proofs of Sample sort, at full length.

Sample sort is correct, and its buckets are small with high probability full

This proof shows: correctness.

Let the keys be distinct. If they are not, pair each key with its input index and order the pairs by key and then by index. The pairs are distinct and sorting them sorts the keys stably, so this case reduces to distinct keys.

Lemma 1. The output is sorted

Proof. Let y0=−∞y_0 = -\infty and yp=+∞y_p = +\infty. By line L4, a key xx is in bucket j=b(x)j = b(x) exactly when yj<x≤yj+1y_j < x \le y_{j+1}, because the splitters y1≤⋯≤yp−1y_1 \le \cdots \le y_{p-1} are sorted. So if xx is in bucket jj and x′x' is in bucket j′>jj' > j, then x≤yj+1≤yj′<x′x \le y_{j+1} \le y_{j'} < x'. Each bucket is sorted on line L6, so the concatenation on line L7 is sorted. Each key goes to exactly one bucket, so the output is a permutation of the input. This holds for every sample. □\square

Lemma 2. A large bucket leaves a run of keys with few draws

Let g≥1g \ge 1. Call a set of gg keys that are consecutive in sorted order a run. If some bucket has more than gg keys, then some run gets at most ss of the psps draws of line L1.

Proof. Let bucket jj have more than gg keys, and let WW be its gg smallest keys. They are consecutive in sorted order, because the bucket holds all the keys of an interval, so WW is a run. Every key of WW is greater than yjy_j and, since the bucket has a larger key, less than yj+1y_{j+1}.

Let a draw have its key in WW. In the sorted sample SS, its position ii satisfies S[i]>yj=S[js]S[i] > y_j = S[js], so i>jsi > js when j≥1j \ge 1, and S[i]<yj+1=S[(j+1)s]S[i] < y_{j+1} = S[(j+1)s], so i<(j+1)si < (j+1)s when j≤p−2j \le p - 2. For j=0j = 0 the positions are less than ss, and for j=p−1j = p - 1 they are greater than (p−1)s(p-1)s and less than psps. In each case the positions lie in a set of at most ss integers, so at most ss draws have their key in WW. □\square

Theorem. Bucket bound

Let p≥2p \ge 2 and g=⌈2n/p⌉≤ng = \lceil 2n/p \rceil \le n. With probability at least 1−ne−s/41 - n e^{-s/4}, every bucket has at most gg keys. For s≥12ln⁡ns \ge 12 \ln n, the probability is at least 1−n−21 - n^{-2}.

Proof. Fix one run WW of gg keys. Each draw falls in WW with probability g/n≥2/pg/n \ge 2/p, independently of the other draws. So the number XX of draws in WW is a binomial variable with mean μ=psg/n≥2s\mu = psg/n \ge 2s. By the Chernoff bound Pr⁡[X≤(1−δ)μ]≤e−δ2μ/2\Pr[X \le (1 - \delta)\mu] \le e^{-\delta^2 \mu/2} with δ=1/2\delta = 1/2,

Pr⁡[X≤s]≤Pr⁡[X≤μ/2]≤e−μ/8≤e−s/4.\Pr[X \le s] \le \Pr[X \le \mu/2] \le e^{-\mu/8} \le e^{-s/4}.

There are n−g+1≤nn - g + 1 \le n runs. By Lemma 2 and a union bound, the probability that some bucket has more than gg keys is at most ne−s/4n e^{-s/4}. For s≥12ln⁡ns \ge 12 \ln n this is at most n⋅n−3=n−2n \cdot n^{-3} = n^{-2}. □\square

Running time

On the CREW PRAM with pp processors, lines L1 to L3 take time O(pslog⁡(ps))O(ps \log(ps)) with the sample sorted on one processor. Line L4 takes time O(⌈n/p⌉log⁡p)O(\lceil n/p \rceil \log p), line L5 takes time O(n/p+p+log⁡p)O(n/p + p + \log p), and on the event of the theorem, line L6 takes time O((n/p+1)log⁡n)O((n/p + 1) \log n). Line L7 is free when the buckets were placed in order on line L5. So with probability at least 1−n−21 - n^{-2} the time is O((n/p)log⁡n+pslog⁡(ps))O((n/p) \log n + ps \log(ps)).

Back to Sample sort