Proofs: Odd-even transposition sort
These are the human proofs of Odd-even transposition sort, at full length.
Odd-even transposition sort sorts in n rounds full
This proof shows: correctness.
The comparators of ODD-EVEN-TRANSPOSITION-SORT do not depend on the keys, so the algorithm is a comparator network. Each comparator only exchanges two keys, so the output is a permutation of the input. By the zero-one principle it is enough to show that the output is sorted for each input in . In the rest of the proof the keys are zeros and ones, positions are , and round applies the comparators with .
Moves of the ones. A comparator changes the array only if and , and then it moves the one from to . So a one moves only to the right, by one position, and only when the position to its right holds a zero. Two ones are never exchanged, so the ones keep their left-to-right order. Let be the number of ones, and number them from the right. Let be the position of one at the start of round . One has ones to its right, so at all times. We call the target of one . The output is sorted exactly when each one is at its target.
Alignment. Say that one is aligned in round when , that is, when the comparator is used in round . One can move in round only if it is aligned, because the other comparator that touches its position, , has a one on its right wire and does not move it. Two facts follow from the definition, since grows by one in each round.
- (A1) If one is not aligned in round , it does not move, so it is aligned in round .
- (A2) If one is aligned in round and moves, then , so it is aligned in round .
Say that one is blocked in round when it is aligned, , and . An aligned one that is not blocked and not at its target moves in round .
Lemma. For each :
- in each round , one is not blocked;
- in each round with , one is aligned, so it moves.
Proof. By induction on .
Part 1. For no one is to the right of one 1, so it is never blocked. Let and , and assume that one is aligned with and . The one at position is the next one to the right, which is one . So , and one is not at its target. Since , part 2 for says that one is aligned in round . But has the other parity, and one is aligned, so one is not aligned. This is a contradiction, so one is not blocked.
Part 2. First take round . In round , one is aligned or not. If it is not aligned, then by (A1) it is aligned in round . If it is aligned and , then by part 1 it is not blocked, so it moves, and by (A2) it is aligned in round . If it is aligned and , then and the claim for round says nothing. Now let and assume that one is aligned in round with . By part 1 it is not blocked, so it moves, and by (A2) it is aligned in round . By induction on , the claim holds for each round in which .
Theorem. After round , the array is sorted.
Proof. Take a one . Positions never decrease, so , and one is at most positions from its target at the start of round . By part 2 of the lemma, in each round from on, one moves one step to the right until it is at its target, and then it stays there, since always. The rounds are rounds, so one is at its target after round . This holds for each , so the ones fill the positions and the zeros fill the positions . The array is sorted. By the zero-one principle the network sorts every input.
Stability. Line L5 exchanges and only when . An exchange of two neighbours changes the relative order of only these two keys, and these two keys are not equal. So equal keys never change their relative order.