Algorithm Search⌘K

Proofs: External samplesort

These are the human proofs of External samplesort, at full length.

External samplesort sorts stably, and its buckets are small with high probability full

This proof shows: correctness.

We use the line numbers of the pseudocode. For a file GG, write st⁡(G)\operatorname{st}(G) for its stable sort.

Correctness

Lemma 1 (buckets). Let s1<⋯<sks_1 < \cdots < s_k be the splitters of line L4, with k≥1k \ge 1, and let U0,E1,U1,…,Ek,UkU_0, E_1, U_1, \ldots, E_k, U_k be the buckets of line L5, each in the order of GG. Then

st⁡(G)=st⁡(U0) E1 st⁡(U1) E2⋯Ek st⁡(Uk).\operatorname{st}(G) = \operatorname{st}(U_0)\, E_1\, \operatorname{st}(U_1)\, E_2 \cdots E_k\, \operatorname{st}(U_k).

Proof. Each item goes to exactly one bucket: either it equals some sjs_j, or it lies strictly between two consecutive splitters, with s0=−∞s_0 = -\infty and sk+1=+∞s_{k+1} = +\infty. Every item of Uj−1U_{j-1} is less than sjs_j, every item of EjE_j equals sjs_j, and every item of UjU_j is greater than sjs_j. So in st⁡(G)\operatorname{st}(G) the items appear grouped in the order U0,E1,U1,…,UkU_0, E_1, U_1, \ldots, U_k. Inside a group, st⁡(G)\operatorname{st}(G) keeps the items of equal keys in the order of GG, and sorts the others; for UjU_j this is st⁡(Uj)\operatorname{st}(U_j), and for EjE_j, whose keys are all equal, it is EjE_j itself in the order of GG. ■\blacksquare

Lemma 2 (progress). If ∣G∣>M|G| > M, each bucket UjU_j has fewer than ∣G∣|G| items.

Proof. The sample is not empty, because aS≥1aS \ge 1, and (S−1)a≥a(S - 1)a \ge a is a rank of the sample, so k≥1k \ge 1. The splitter s1s_1 is an item of GG, and it is in E1E_1, not in any UjU_j. ■\blacksquare

Theorem. EXTERNAL-SAMPLESORT returns st⁡(F)\operatorname{st}(F).

Proof. By strong induction on NN. If N≤MN \le M, line L1 returns the stable sort of FF. Else, by Lemma 2, each recursive call of line L7 is on a smaller file, so by induction it returns st⁡(Uj)\operatorname{st}(U_j). Line L8 writes the buckets in the order of Lemma 1, which is st⁡(F)\operatorname{st}(F). ■\blacksquare

The size of the buckets

Lemma 3. Let S≥2S \ge 2 and a≥8ln⁡Sa \ge 8 \ln S, let the sample be aSaS positions of GG drawn uniformly and independently, and let N=∣G∣N = |G| and L=⌈2N/S⌉L = \lceil 2N/S \rceil. Then, with probability at least 1−1/(2S)1 - 1/(2S), each bucket UjU_j has at most 2L−22L - 2 items, that is, fewer than 4N/S+24N/S + 2.

Proof. Order the items of GG by key, and by position for equal keys, and number them 0,…,N−10, \ldots, N - 1 in this order. Each UjU_j is a set of consecutive numbers, because it is the set of items with keys in an open interval.

Few sample items in a bucket. The chosen ranks of line L4 are a,2a,…,(S−1)aa, 2a, \ldots, (S - 1)a in the sorted sample of aSaS items. For 1≤j<k1 \le j < k, let rr be the largest chosen rank with the value sjs_j. The next chosen rank, r+ar + a, has a value that is greater than sjs_j and is a splitter, so it is sj+1s_{j+1}. A sample item in UjU_j has a value strictly between sjs_j and sj+1s_{j+1}, so its rank is strictly between rr and r+ar + a: at most a−1a - 1 ranks. In the same way, a sample item in U0U_0 has a rank below the first chosen rank aa, and one in UkU_k has a rank above the largest chosen rank with the value sks_k, which is (S−1)a(S - 1)a. So each UjU_j holds at most aa sample items.

A chunk with few sample items. Cut the numbers into the chunks Cc={cL,…,cL+L−1}C_c = \{cL, \ldots, cL + L - 1\} for c=0,1,…c = 0, 1, \ldots, and call a chunk full if it lies inside {0,…,N−1}\{0, \ldots, N - 1\}. There are at most N/L≤S/2N/L \le S/2 full chunks. A set of at least 2L−12L - 1 consecutive numbers contains a full chunk: the first multiple of LL in it is at most L−1L - 1 after its start, and the chunk that begins there ends at most 2L−22L - 2 after its start. So if some UjU_j has 2L−12L - 1 or more items, some full chunk holds at most aa sample items.

The probability. For a fixed full chunk, the number XX of sample positions in it is binomial with aSaS trials and success probability L/N≥2/SL/N \ge 2/S, so μ=E[X]≥2a\mu = \mathbb{E}[X] \ge 2a. 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≤a]≤Pr⁡[X≤μ/2]≤e−μ/8≤e−a/4≤S−2.\Pr[X \le a] \le \Pr[X \le \mu/2] \le e^{-\mu/8} \le e^{-a/4} \le S^{-2}.

A union bound over at most S/2S/2 full chunks gives a probability of at most 1/(2S)1/(2S) that some full chunk holds at most aa sample positions. ■\blacksquare

Corollary. If S≥4N/MS \ge 4N/M, then with probability at least 1−1/(2S)1 - 1/(2S) every bucket UjU_j fits in memory, so each recursive call of line L7 stops at line L1.

Proof. 2N/S≤M/22N/S \le M/2, so L≤⌈M/2⌉L \le \lceil M/2 \rceil and 2L−2≤M2L - 2 \le M. ■\blacksquare

Back to External samplesort