Algorithm Search⌘K

Proofs: Balance Sort

These are the human proofs of Balance Sort, at full length.

Balance Sort sorts stably, and keeps every bucket balanced across the disks full

This proof shows: correctness.

We use the line numbers of the pseudocode. All items carry their input position, and ties on the key are broken by the position, so all items are distinct and their order is a total order. A sort of the items in this order is a stable sort of the keys.

Correctness

Lemma 1 (buckets). Let s1<⋯<sks_1 < \cdots < s_k be the splitters, s0=−∞s_0 = -\infty and sk+1=+∞s_{k+1} = +\infty. Lines L6 and L9 put each item xx of FF in exactly one bucket jj, the one with sj≤x<sj+1s_j \le x < s_{j+1}. Every item of bucket jj is smaller than every item of bucket j′j' for j<j′j < j'.

Proof. The intervals [sj,sj+1)[s_j, s_{j+1}) for j=0,…,kj = 0, \ldots, k are disjoint and cover all values. A write step of line L7 either places a ready block or keeps it ready, and line L9 repeats write steps until no block is ready, so every item is written to its bucket once. If xx is in bucket jj and yy in bucket j′>jj' > j, then x<sj+1≤sj′≤yx < s_{j+1} \le s_{j'} \le y. ■\blacksquare

Lemma 2 (progress). If N>MN > M, then k≥1k \ge 1 and every bucket has fewer than NN items.

Proof. The first memoryload has ⌊M/B⌋B≥M−B+1\lfloor M/B \rfloor B \ge M - B + 1 items, which is at least 5M/65M/6 because M≥6BM \ge 6B. The step of the sample is q=max⁡(2,⌈M/(4S)⌉)q = \max(2, \lceil M/(4S) \rceil), and 2q≤max⁡(4,2⌈M/8⌉)≤5M/62q \le \max(4, 2\lceil M/8 \rceil) \le 5M/6 for M≥6M \ge 6. So this load has at least 2q2q items and gives at least 2 sample items. The rank r≥2r \ge 2 is at most the sample size, so k≥1k \ge 1. The splitter s1s_1 has rank r≥2r \ge 2 in the sample, so the sample item yy of rank 1 is smaller than s1s_1 and lies in bucket 0. So every bucket j≥1j \ge 1 misses yy. Bucket 0 misses s1s_1, which is an item of FF. ■\blacksquare

Theorem 1. BALANCE-SORT returns the items of FF in sorted order.

Proof. By strong induction on NN. For N≤MN \le M, line L1 sorts in memory. Else, by Lemma 2, each bucket is smaller, so by induction line L9 sorts it. By Lemma 1, the buckets in order, each sorted, form the sorted file. ■\blacksquare

Balance

Fix a bucket bb, and write XX for XbX_b and X(d)X(d) for Xb(d)X_b(d). The invariant is X(d)≤2⌈X/D⌉X(d) \le 2\lceil X/D \rceil for all dd.

Lemma 3. Before each placement for bucket bb, more than D/2D/2 disks are allowed for bb, not counting the disks already used in the same write step.

Proof. Let c=⌈(X+1)/D⌉≥1c = \lceil (X + 1)/D \rceil \ge 1. A disk dd is not allowed if X(d)+1>2cX(d) + 1 > 2c, that is, X(d)≥2cX(d) \ge 2c. If tt disks are not allowed, then 2ct≤∑dX(d)=X≤cD−12ct \le \sum_d X(d) = X \le cD - 1, because X+1≤cDX + 1 \le cD. So t<D/2t < D/2, and more than D/2D/2 disks are allowed. ■\blacksquare

Lemma 4 (invariant). After each placement, Xb(d)≤2⌈Xb/D⌉X_b(d) \le 2\lceil X_b/D \rceil for all buckets bb and disks dd.

Proof. At the start all counts are 0. A placement of a block of bb on an allowed disk dd makes Xb(d)X_b(d) at most 2⌈(Xb+1)/D⌉2\lceil (X_b + 1)/D\rceil, which is 2⌈Xb′/D⌉2\lceil X_b'/D \rceil for the new total Xb′=Xb+1X_b' = X_b + 1. The other counts of bb do not change, and ⌈Xb/D⌉\lceil X_b/D \rceil does not decrease. The counts of the other buckets do not change. ■\blacksquare

Theorem 2. Each write step places at least min⁡(R,⌊D/2⌋+1)\min(R, \lfloor D/2 \rfloor + 1) blocks, where RR is the number of ready blocks at its start. At the end of the pass, each bucket of XX blocks can be read in at most 2⌈X/D⌉2\lceil X/D \rceil parallel reads.

Proof. Suppose that a write step has placed u<⌊D/2⌋+1u < \lfloor D/2 \rfloor + 1 blocks when it looks at the next ready block, of bucket bb. Then u≤D/2u \le D/2 disks are used. By Lemma 3, more than D/2D/2 disks are allowed for bb, so at least one of them is unused, and line L7 places the block. So the step places blocks as long as fewer than ⌊D/2⌋+1\lfloor D/2 \rfloor + 1 are placed and ready blocks remain. For the second part, a parallel read takes one block from each disk, so a bucket can be read in max⁡dX(d)\max_d X(d) steps, which is at most 2⌈X/D⌉2\lceil X/D \rceil by Lemma 4. ■\blacksquare

Back to Balance Sort