Proofs: Pigeonhole sort
These are the human proofs of Pigeonhole sort, at full length.
Pigeonhole sort sorts the items and is stable full
This proof shows: correctness, stability, termination.
Let (for line L1 returns, and the empty array is sorted). Let for the input array, , and .
Step 1 (the holes exist). For each , , so . So line L6 appends to one of the holes made by line L4.
Step 2 (the scatter). At the test of line L5 with index , for each , the hole holds the items with and , in increasing order of . For all holes are empty. Step appends at the end of and changes no other hole, so the claim holds for . At , hole holds all items with key , in input order. The array is not written in this loop, so it still holds the input.
Step 3 (the gather). At the start of the step of line L8 with index , , and holds the items of , each hole in its order, one hole after the other. For , by line L7. The loop of lines L9 and L10 writes the items of to in order and adds to , so the claim holds for . The writes do not lose data, because all items are in the holes after Step 2.
Step 4 (the result). At the end, and . So holds each input item exactly once. Its keys are non-decreasing, because the holes are written in increasing order of and hole holds only the key . Two items with the same key are in the same hole, in input order by Step 2, and they are written in that order. So the sort is stable.
Termination. Each loop has a fixed number of steps: for line L5, for line L8, and for line L9, with .