Algorithm Search⌘K

Proofs: Parallel sorting by regular sampling

These are the human proofs of Parallel sorting by regular sampling, at full length.

PSRS sorts, and every processor gets at most 2n/p − p keys full

This proof shows: correctness.

Let the nn keys be distinct, let p2p^2 divide nn and let w=n/p2w = n/p^2, so each block has pwpw keys. Equal keys reduce to this case by pairing each key with its input index and ordering the pairs by key, then index.

Lemma 1. The output is sorted

Proof. Let y0=−∞y_0 = -\infty and yp=+∞y_p = +\infty. The samples are sorted, so y1≤⋯≤yp−1y_1 \le \cdots \le y_{p-1}. Each key xx lies in exactly one bucket jj, the one with yj<x≤yj+1y_j < x \le y_{j+1}. 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 block is sorted, so its keys of bucket jj form a sorted, contiguous piece, found on line L4 by binary search. Line L6 merges the pieces of bucket jj into a sorted list of all its keys. So the buckets, in order, are the sorted output. □\square

Lemma 2. Each bucket contains exactly p samples

Proof. The keys are distinct, so the samples are distinct, and S[0]<S[1]<⋯<S[p2−1]S[0] < S[1] < \cdots < S[p^2 - 1]. With yj=S[jp−1]y_j = S[jp - 1], the samples in bucket jj are those with S[jp−1]<S[t]≤S[(j+1)p−1]S[jp - 1] < S[t] \le S[(j+1)p - 1], where for j=0j = 0 the lower bound is −∞-\infty and for j=p−1j = p - 1 the upper bound is +∞+\infty, which is S[p2−1]S[p^2 - 1] for the samples. These are S[jp],…,S[(j+1)p−1]S[jp], \ldots, S[(j+1)p - 1], which are pp samples. □\square

Lemma 3. A stretch of a block with c samples has at most (c + 1)w − 1 keys

Within a sorted block, the samples are at positions 0,w,2w,…,(p−1)w0, w, 2w, \ldots, (p-1)w. Let a stretch of consecutive positions L,…,RL, \ldots, R contain cc of these positions. Then R−L+1≤(c+1)w−1R - L + 1 \le (c + 1)w - 1.

Proof. If c=0c = 0, the stretch lies between two consecutive sample positions tw<L≤R<(t+1)wtw < L \le R < (t+1)w, or after the last one, (p−1)w<L≤R≤pw−1(p-1)w < L \le R \le pw - 1. In both cases it has at most w−1w - 1 positions. If c≥1c \ge 1, let the sample positions in the stretch be tw,…,(t+c−1)wtw, \ldots, (t + c - 1)w. The stretch starts after the previous sample position, so L≥(t−1)w+1L \ge (t-1)w + 1, or L≥0=twL \ge 0 = tw if t=0t = 0. In both cases L≥tw−w+1L \ge tw - w + 1. It ends before the next sample position, so R≤(t+c)w−1R \le (t + c)w - 1, which also holds at the end of the block, where R≤pw−1R \le pw - 1 and t+c≤pt + c \le p. So R−L+1≤(t+c)w−1−(tw−w+1)+1=(c+1)w−1R - L + 1 \le (t + c)w - 1 - (tw - w + 1) + 1 = (c + 1)w - 1. □\square

Theorem. Bucket bound

Every bucket has at most 2n/p−p2n/p - p keys.

Proof. Fix bucket jj. In block bb, the keys of bucket jj form a stretch of consecutive positions, by Lemma 1. Let cbc_b be the number of samples of block bb in this stretch. The samples of a block lie in the bucket exactly when they lie in this stretch, so by Lemma 2, ∑bcb=p\sum_b c_b = p. By Lemma 3, the bucket has at most

∑b=0p−1((cb+1)w−1)=(p+p)w−p=2pw−p=2np−p\sum_{b=0}^{p-1} \big((c_b + 1) w - 1\big) = (p + p) w - p = 2pw - p = \frac{2n}{p} - p

keys. □\square

The example on the page has n=27n = 27 and p=3p = 3, and its third bucket has 15=2⋅9−315 = 2 \cdot 9 - 3 keys, so for these values of nn and pp the bound is reached.

Back to Parallel sorting by regular sampling