Proofs: Cycle sort
These are the human proofs of Cycle sort, at full length.
Cycle sort sorts the array and writes each misplaced position exactly once full
This proof shows: correctness, termination.
Let be the input in sorted order. Call a position final when , where means that neither item is less than the other. For let be the multiset .
Outer invariant. At the start of the iteration of line L2 for :
(O) , and the multiset of is .
It holds for because is a permutation of .
Lemma 1 (POSITION). Let be the number of items of that are less than , and suppose the multiset of plus is . Then POSITION returns , and are the copies of in , where is their number.
Proof. Lines L13 to L15 add 1 for each item of less than , so they return . The items of less than are all in , because is not less than itself, so exactly items of are less than . In the sorted order of they take the places , and the copies of come next.
Inner invariant. Fix with (O), and let at line L3. If line L5 continues, then for , so by Lemma 1 is a copy of , and (O) holds for . Otherwise, at each test of line L7 of this cycle:
(I) The multiset of plus is ; still holds ; each final position of at the start of the cycle is still final; and is the value of Lemma 1 for the held .
At the first test, (I) holds by line L4 and because nothing has been written.
Lemma 2 (one step). Assume (I) at line L7. Then line L7 stops at a position in the block of with , and after line L8, position is final, no final position of has lost its item, and the multiset of plus the new is again if .
Proof. First let . Then is a smallest item of , and : if were a copy of , it would also be a smallest item of , which would give for , and line L5 would have continued. So line L7 stops at once at . Now let , so the block lies in . The array part holds copies of , because the held item is the -th copy, so some position of the block does not hold , and line L7, which moves right from the first position of the block past copies of only, stops at the first such position . In both cases is a copy of by Lemma 1, so after line L8 places at , the position is final. Before the write , so was not final, and no final item was moved. If , the write exchanges an item of with the held item, which keeps the multiset of the two together.
Termination of a cycle. By Lemma 2, each write at a position turns a position of that was not final into a final one, and no final position becomes not final. There are positions there, so after at most such writes the next write is at , and line L10 ends the loop. Between writes, line L11 sets by Lemma 1, so (I) holds at the next test of line L7.
End of a cycle. The last write is at , which by Lemma 2 needs , so the held item is a smallest item of and becomes . The stale copy is now the held item and is dropped. Before this write, the array part plus the held item was , so after it is , with . So (O) holds for .
Conclusion. The loop of line L2 ends after . By (O) for , and is the one item of . So , and the array is sorted.
Writes. Each write makes a position final that was not final, and no position stops being final, except that is not final while it holds the stale copy , and it is written once, at the end of its cycle. A position that is final at the start of the run is never written: a position is written only when it is not final, and a start that is final makes line L5 continue. So each position is written at most once, exactly the positions that are not final at the start are written, and the return value of line L12 is the number of positions with in the input. Any algorithm that sorts the array must write each of these positions at least once, so the number is the least possible.