Proofs: Stooge sort
These are the human proofs of Stooge sort, at full length.
Stooge sort sorts the range and keeps its items full
This proof shows: correctness.
Each change to the array is the swap of line L1, inside the range of the call. So each call keeps the multiset of the items of its range and changes no item outside it. To speak of "the largest items" when there are equal keys, we fix a strict total order on the items: compare by key, and on a tie by the index that the item had at the start of the call. An array is sorted by key when it is sorted in this order, so it is enough to prove that the call sorts the range in this order.
Theorem. For each range with , STOOGE-SORT sorts the range.
Proof. By strong induction on .
Base. If , the range is sorted, and line L1 compares the item with itself and does nothing. If , line L1 swaps the two items when they are out of order, and line L2 stops. So the range is sorted.
Step. Let , and assume the claim for all ranges with fewer than items. Line L1 changes the multiset of no part, so we ignore it. Let . Then , so , and . The three calls of lines L4, L5 and L6 are on ranges of items, so by the hypothesis each one sorts its range.
Let be the set of the largest items of the range. Before line L4, let be the number of items of in the first part ; the other items of are in , which line L4 does not change. After line L4, is sorted, so its largest items are in its last positions, . Each item of is larger than each item not in , so these items are exactly the items of in . Since and , the first of these positions is , because . So after line L4, all items of are in .
Line L5 sorts . Its largest items are the items of , so after line L5 they are in the last positions , in sorted order. The other items, none of them in , are in .
Line L6 sorts and does not change . Now is sorted, is sorted, and each item of the first part is not in , so it is less than each item of the second part. So the range is sorted.
Termination. Each call on items makes three calls on items, and a call on at most two items makes no call. So the recursion stops.
Why the rounding matters. The step used . With this fails for : then and . On the calls sort , and , which change nothing, and the output is not sorted.