Proofs: Cycle-leader in-place permutation
These are the human proofs of Cycle-leader in-place permutation, at full length.
APPLY and APPLY-READONLY move every item to its target and restore P full
This proof shows: correctness, termination.
We use the pseudocode of the page. Let be a permutation of , let be the array on entry, and let the goal be the array with for every , equivalently . For an index write for its cycle and , so and the indices are distinct. Write for the value of the entry of at ignoring its sign.
Lemma 1 (one cycle). Suppose the cycle walk of lines L3 to L7 starts at when for every and when for every . Then it ends after rounds of the loop at line L4, with for every , with unchanged outside , and with the entries of on negated and all other entries unchanged.
Proof. We claim that before round of the loop, for , we have , , and for , and every other entry of and on is as on entry. For , line L3 sets and , and nothing else has changed. Suppose the claim holds before round and , so and has not been written yet. Line L5 stores at , which is by the definition of , and sets to the old content . Line L6 reads , still positive since was not marked, negates and sets . So the claim holds before round . The loop stops at the first with , which is . Then , which line L7 writes to , so , and line L7 negates . Every index of has now been written once and negated once, and nothing outside was read for writing or changed.
Lemma 2 (APPLY). Before iteration of line L1, the cycles that contain an index smaller than are exactly the cycles whose entries of are all negative, they hold their final items, and every other cycle holds its original items and has all its entries of positive.
Proof. By induction on . Before nothing is marked and nothing has moved. At iteration , if contains an index smaller than then by the hypothesis its entries, including , are negative, line L2 skips it, and the statement holds for . Otherwise is the smallest index of , the hypothesis says that holds its original items with positive entries, and the absolute values of all entries equal . Lemma 1 then finishes and marks it, without touching the other cycles. In both cases the cycles with an index smaller than are exactly the finished, negated ones.
Theorem 1. APPLY returns and leaves unchanged.
Proof. After the last iteration of line L1, Lemma 2 with says every cycle is finished, so , and every entry of is negative with absolute value . Line L8 negates every entry once, which restores .
Lemma 3 (leader test). At iteration of line L10, line L11 ends with exactly when .
Proof. The walk visits and stops at the first visited index that is at most . Such an index exists, since , so the walk stops after at most steps. If , every index visited before returning to is larger than , so the walk stops at . If some index of is smaller than , the walk meets one of them before it can come back to , since it passes through every element of before , and stops there with .
Theorem 2. APPLY-READONLY returns and does not write .
Proof. Line L14 is the walk of Lemma 1 without the marks. The argument of Lemma 1 does not use the signs except to keep readable, so a walk started at when holds its original items finishes and changes nothing else. By Lemma 3, each cycle is walked exactly once, at its smallest index, and at that moment no index of it has been written, because the only walks so far started at the leaders of other cycles. So at the end every cycle is finished and . No line writes .
Termination. Each walk of line L4 or line L14 stops after rounds by Lemma 1, each walk of line L11 after at most steps by Lemma 3, and the loops at lines L1, L8 and L10 run times. So both procedures halt, APPLY after steps in total, since the rounds of line L4 over all cycles add up to , where is the number of cycles.