Algorithm Search⌘K

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 {0,1}n\{0, 1\}^n. In the rest of the proof the keys are zeros and ones, positions are 0,…,n−10, \ldots, n-1, and round tt applies the comparators [i:i+1][i : i+1] with i≡t−1(mod2)i \equiv t - 1 \pmod 2.

Moves of the ones. A comparator [i:i+1][i : i+1] changes the array only if A[i]=1A[i] = 1 and A[i+1]=0A[i+1] = 0, and then it moves the one from ii to i+1i + 1. 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 kk be the number of ones, and number them 1,…,k1, \ldots, k from the right. Let Pj(t)P_j(t) be the position of one jj at the start of round tt. One jj has j−1j - 1 ones to its right, so Pj(t)≤n−jP_j(t) \le n - j at all times. We call n−jn - j the target of one jj. The output is sorted exactly when each one jj is at its target.

Alignment. Say that one jj is aligned in round tt when Pj(t)≡t−1(mod2)P_j(t) \equiv t - 1 \pmod 2, that is, when the comparator [Pj(t):Pj(t)+1][P_j(t) : P_j(t) + 1] is used in round tt. One jj can move in round tt only if it is aligned, because the other comparator that touches its position, [Pj(t)−1:Pj(t)][P_j(t) - 1 : P_j(t)], has a one on its right wire and does not move it. Two facts follow from the definition, since tt grows by one in each round.

  • (A1) If one jj is not aligned in round tt, it does not move, so it is aligned in round t+1t + 1.
  • (A2) If one jj is aligned in round tt and moves, then Pj(t+1)=Pj(t)+1P_j(t+1) = P_j(t) + 1, so it is aligned in round t+1t + 1.

Say that one jj is blocked in round tt when it is aligned, Pj(t)<n−jP_j(t) < n - j, and A[Pj(t)+1]=1A[P_j(t) + 1] = 1. An aligned one that is not blocked and not at its target moves in round tt.

Lemma. For each j∈{1,…,k}j \in \{1, \ldots, k\}:

  1. in each round t≥jt \ge j, one jj is not blocked;
  2. in each round t≥j+1t \ge j + 1 with Pj(t)<n−jP_j(t) < n - j, one jj is aligned, so it moves.

Proof. By induction on jj.

Part 1. For j=1j = 1 no one is to the right of one 1, so it is never blocked. Let j≥2j \ge 2 and t≥jt \ge j, and assume that one jj is aligned with Pj(t)<n−jP_j(t) < n - j and A[Pj(t)+1]=1A[P_j(t) + 1] = 1. The one at position Pj(t)+1P_j(t) + 1 is the next one to the right, which is one j−1j - 1. So Pj−1(t)=Pj(t)+1<n−(j−1)P_{j-1}(t) = P_j(t) + 1 < n - (j - 1), and one j−1j - 1 is not at its target. Since t≥j=(j−1)+1t \ge j = (j - 1) + 1, part 2 for j−1j - 1 says that one j−1j - 1 is aligned in round tt. But Pj−1(t)=Pj(t)+1P_{j-1}(t) = P_j(t) + 1 has the other parity, and one jj is aligned, so one j−1j - 1 is not aligned. This is a contradiction, so one jj is not blocked.

Part 2. First take round j+1j + 1. In round jj, one jj is aligned or not. If it is not aligned, then by (A1) it is aligned in round j+1j + 1. If it is aligned and Pj(j)<n−jP_j(j) < n - j, then by part 1 it is not blocked, so it moves, and by (A2) it is aligned in round j+1j + 1. If it is aligned and Pj(j)=n−jP_j(j) = n - j, then Pj(j+1)=n−jP_j(j+1) = n - j and the claim for round j+1j + 1 says nothing. Now let t≥j+1t \ge j + 1 and assume that one jj is aligned in round tt with Pj(t)<n−jP_j(t) < n - j. By part 1 it is not blocked, so it moves, and by (A2) it is aligned in round t+1t + 1. By induction on tt, the claim holds for each round t≥j+1t \ge j + 1 in which Pj(t)<n−jP_j(t) < n - j. □\square

Theorem. After round nn, the array is sorted.

Proof. Take a one jj. Positions never decrease, so Pj(j+1)≥0P_j(j+1) \ge 0, and one jj is at most n−jn - j positions from its target at the start of round j+1j + 1. By part 2 of the lemma, in each round from j+1j + 1 on, one jj moves one step to the right until it is at its target, and then it stays there, since Pj≤n−jP_j \le n - j always. The rounds j+1,…,nj + 1, \ldots, n are n−jn - j rounds, so one jj is at its target after round nn. This holds for each jj, so the ones fill the positions n−k,…,n−1n - k, \ldots, n - 1 and the zeros fill the positions 0,…,n−k−10, \ldots, n - k - 1. The array is sorted. By the zero-one principle the network sorts every input. ■\blacksquare

Stability. Line L5 exchanges A[i]A[i] and A[i+1]A[i+1] only when A[i]>A[i+1]A[i] > A[i+1]. 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.

Back to Odd-even transposition sort