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 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 of integers, HAN-SORT returns the keys of , with their counts, in non-decreasing order.
Proof. Induction on .
Base. If , line L1 returns the result of insertion sort, which is correct.
Step. Let , and assume the claim for all lists with fewer than keys. The sample has about keys (for ), so by the hypothesis line L4 returns sorted, and after the removal of duplicates with , since is not empty.
Each key goes to exactly one set. For a key , either for one , and then , or differs from all splitters, and then there is exactly one with , with and , and . So the sets split the keys of , with their counts.
Each part is smaller. Each is a key of , and no key of is equal to a splitter. So , since contains . By the hypothesis, line L7 sorts each .
The order. Let come before in the output of line L8. If they are in the same set, then either the set is some and , or the set is some , which is sorted, so . If they are in different sets, the set of comes first in the order . Each key of is less than , each key of equals , and each key of is greater than . 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 .
So the output holds the keys of 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.