Algorithm Search⌘K

Proofs: Han's integer sorting

These are the human proofs of Han's integer sorting, at full length.

The splitter recursion of Han's sort returns its input in sorted order full

This proof shows: correctness, termination.

We prove the claim for the recursion of the pseudocode, for any sample that line L3 takes from AA and any method of line L5 that finds the correct part of each key. The speed of Han's algorithm comes from how it does lines L3 and L5; the correctness does not depend on it.

Claim. For each finite list AA of integers, HAN-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, which is correct.

Step. Let n>cn > c, and assume the claim for all lists with fewer than nn keys. The sample PP has about n/k≤n+1<nn / k \le \sqrt{n} + 1 < n keys (for n>c≥2n > c \ge 2), so by the hypothesis line L4 returns PP sorted, and after the removal of duplicates p1<⋯<psp_1 < \cdots < p_s with s≥1s \ge 1, since PP is not empty.

Each key goes to exactly one set. For a key xx, either x=pjx = p_j for one jj, and then x∈Ejx \in E_j, or xx differs from all splitters, and then there is exactly one j∈{0,…,s}j \in \{0, \ldots, s\} with pj<x<pj+1p_j < x < p_{j+1}, with p0=−∞p_0 = -\infty and ps+1=+∞p_{s+1} = +\infty, and x∈Bjx \in B_j. So the sets E1,…,Es,B0,…,BsE_1, \ldots, E_s, B_0, \ldots, B_s split the keys of AA, with their counts.

Each part is smaller. Each pjp_j is a key of AA, and no key of B0∪⋯∪BsB_0 \cup \cdots \cup B_s is equal to a splitter. So ∣Bj∣≤n−∣E1∣<n|B_j| \le n - |E_1| < n, since E1E_1 contains p1p_1. By the hypothesis, line L7 sorts each BjB_j.

The order. Let xx come before yy in the output of line L8. If they are in the same set, then either the set is some EjE_j and x=y=pjx = y = p_j, or the set is some BjB_j, which is sorted, so x≤yx \le y. If they are in different sets, the set of xx comes first in the order B0,E1,B1,…,Es,BsB_0, E_1, B_1, \ldots, E_s, B_s. Each key of Bj−1B_{j-1} is less than pjp_j, each key of EjE_j equals pjp_j, and each key of BjB_j is greater than pjp_j. So the sets are in increasing order of their keys: all keys of a set are less than all keys of each later set. Hence x<yx < y.

So the output holds the keys of AA with their counts, and it is in non-decreasing order.

Termination. Each call makes calls only on lists with fewer keys (the sample and the parts), so the recursion is finite. ■\blacksquare

Back to Han's integer sorting