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 of integers and each sequence of random choices, HAN-THORUP-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.
Step. Let , and assume the claim for all lists with fewer than 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 keys, all drawn from , so by the hypothesis line L4 returns it sorted, and the splitters , with , are keys of .
Each key goes to exactly one set. With and , a key is either equal to exactly one , and goes to , or lies strictly between and for exactly one , and goes to . So the sets split the keys of with their counts.
Each part is smaller. No key of a part equals , and is a key of . So , and by the hypothesis line L7 sorts each correctly.
The order. All keys of are less than , all keys of equal , and all keys of are greater than . So in the order each key of a set is less than each key of a later set. Inside the keys are equal, and inside they are sorted. So the output of line L8 is non-decreasing, and it holds the keys of 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.