Proofs: Flashsort
These are the human proofs of Flashsort, at full length.
Flashsort sorts its input full
This proof shows: correctness, termination.
Assume and ; otherwise lines L1 and L2 return an array that is already sorted. The class map is non-decreasing, with and , and for . Let be the count of line L4, the value of after line L5, and . The blocks are disjoint, in the order of , and cover .
Call position placed if for the class whose block contains . Placed positions hold keys of the class of their block, as we show.
Invariant (P). At each test of line L7:
(P1) each placed position in block holds a key of class ; (P2) for each , the number of keys of class at positions that are not placed is ; (P3) every position is placed; (P4) is a permutation of the input.
At the start no position is placed, , and there are keys of class , so (P1) to (P4) hold with .
Line L8. Let , let be the block that contains , and let . If is placed, then by (P1), and , so line L8 moves on, and (P3) holds for . If is not placed, then . By (P3), all positions of are placed, and since the placed positions of block are , this means . Now : if , then the block of lies before and is fully placed by (P3), so , and by (P2) there is no key of class at a position that is not placed, but is one. If , then . If , then . So in both cases line L8 does not skip, and line L9 starts a cycle with .
The cycle of line L10. During the cycle, position is treated as empty: its content is the leader, which is also in hand. We claim that before each step, the key in hand is a key of some class that is not yet placed, and is a position that is not placed, at or after . By (P2) applied with counted at its old position, , so is in block and not placed. If 's block lies before , it is fully placed by (P3), which contradicts the existence of , so . The step decreases , so the position is now placed and holds , a key of class : (P1) holds. The key that was there was at a position that was not placed; it is now in hand, and (P2) holds for its class, because it is counted as not placed until it is put down. If the position was , the key that was there is the stale copy of the leader, which is already accounted for, and the cycle ends with placed. Each step places one more position, and there are finitely many, so the cycle ends. The cycle can end only at position , so when it ends, is placed and holds a key of its block; the leader was put down in the cycle at its own place; and each key of the input is again at exactly one position, so (P4) holds. Then (P3) holds for at the next test.
Termination of the loop. Each turn of line L7 either increases or runs a cycle that places at least one position and then the next turn increases at line L8. So the loop ends with .
After the loop. By (P3) every position is placed, and by (P1) block holds keys of class . Since is non-decreasing, a key of a lower class is not larger than a key of a higher class. So the array is a permutation of the input in which every inversion is between two keys of the same block.
Line L11. Insertion sort sorts any array, so the result is sorted.