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 , write for its stable sort.
Correctness
Lemma 1 (buckets). Let be the splitters of line L4, with , and let be the buckets of line L5, each in the order of . Then
Proof. Each item goes to exactly one bucket: either it equals some , or it lies strictly between two consecutive splitters, with and . Every item of is less than , every item of equals , and every item of is greater than . So in the items appear grouped in the order . Inside a group, keeps the items of equal keys in the order of , and sorts the others; for this is , and for , whose keys are all equal, it is itself in the order of .
Lemma 2 (progress). If , each bucket has fewer than items.
Proof. The sample is not empty, because , and is a rank of the sample, so . The splitter is an item of , and it is in , not in any .
Theorem. EXTERNAL-SAMPLESORT returns .
Proof. By strong induction on . If , line L1 returns the stable sort of . Else, by Lemma 2, each recursive call of line L7 is on a smaller file, so by induction it returns . Line L8 writes the buckets in the order of Lemma 1, which is .
The size of the buckets
Lemma 3. Let and , let the sample be positions of drawn uniformly and independently, and let and . Then, with probability at least , each bucket has at most items, that is, fewer than .
Proof. Order the items of by key, and by position for equal keys, and number them in this order. Each 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 in the sorted sample of items. For , let be the largest chosen rank with the value . The next chosen rank, , has a value that is greater than and is a splitter, so it is . A sample item in has a value strictly between and , so its rank is strictly between and : at most ranks. In the same way, a sample item in has a rank below the first chosen rank , and one in has a rank above the largest chosen rank with the value , which is . So each holds at most sample items.
A chunk with few sample items. Cut the numbers into the chunks for , and call a chunk full if it lies inside . There are at most full chunks. A set of at least consecutive numbers contains a full chunk: the first multiple of in it is at most after its start, and the chunk that begins there ends at most after its start. So if some has or more items, some full chunk holds at most sample items.
The probability. For a fixed full chunk, the number of sample positions in it is binomial with trials and success probability , so . By the Chernoff bound with ,
A union bound over at most full chunks gives a probability of at most that some full chunk holds at most sample positions.
Corollary. If , then with probability at least every bucket fits in memory, so each recursive call of line L7 stops at line L1.
Proof. , so and .