Algorithm Search⌘K

Proofs: Han-Thorup randomized integer sorting

These are the human proofs of Han-Thorup randomized integer sorting, at full length.

The Han-Thorup splitter recursion returns its input in sorted order, whatever its random choices full

This proof shows: correctness, termination.

We prove the claim for the recursion of the pseudocode, as the model runs it: line L6 draws at most three new samples, and then it accepts the last one. The claim holds for every outcome of the random choices, so the algorithm is Las Vegas.

Claim. For each finite list AA of integers and each sequence of random choices, HAN-THORUP-SORT(A)(A) returns the keys of AA, with their counts, in non-decreasing order.

Proof. Induction on n=∣A∣n = |A|.

Base. If n≤cn \le c, line L1 returns the result of insertion sort.

Step. Let n>c≥2n > c \ge 2, and assume the claim for all lists with fewer than nn keys and all random choices. The loop of lines L3 to L6 runs at most four times, so it ends. Consider the sample that it accepts. It has k=⌈n⌉<nk = \lceil \sqrt{n} \rceil < n keys, all drawn from AA, so by the hypothesis line L4 returns it sorted, and the splitters p1<⋯<psp_1 < \cdots < p_s, with s≥1s \ge 1, are keys of AA.

Each key goes to exactly one set. With p0=−∞p_0 = -\infty and ps+1=+∞p_{s+1} = +\infty, a key xx is either equal to exactly one pjp_j, and goes to EjE_j, or lies strictly between pjp_j and pj+1p_{j+1} for exactly one j∈{0,…,s}j \in \{0, \ldots, s\}, and goes to BjB_j. So the sets split the keys of AA with their counts.

Each part is smaller. No key of a part BjB_j equals p1p_1, and p1p_1 is a key of AA. So ∣Bj∣<n|B_j| < n, and by the hypothesis line L7 sorts each BjB_j correctly.

The order. All keys of Bj−1B_{j-1} are less than pjp_j, all keys of EjE_j equal pjp_j, and all keys of BjB_j are greater than pjp_j. So in the order B0,E1,B1,…,Es,BsB_0, E_1, B_1, \ldots, E_s, B_s each key of a set is less than each key of a later set. Inside EjE_j the keys are equal, and inside BjB_j they are sorted. So the output of line L8 is non-decreasing, and it holds the keys of AA with their counts.

Termination. Each call runs a bounded loop and then calls itself only on lists with fewer keys, so every run ends, for every sequence of random choices. ■\blacksquare

Back to Han-Thorup randomized integer sorting