Algorithm Search⌘K

Proofs: Odd-even merge sort

These are the human proofs of Odd-even merge sort, at full length.

Odd-even merge sort sorts each input full

This proof shows: correctness.

The calls of OEM-SORT and ODD-EVEN-MERGE make the same comparators on each input, so they form a comparator network of standard comparators. Each comparator only exchanges two items, 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 of zeros and ones.

For a call ODD-EVEN-MERGE(A,lo,m,r)(A, lo, m, r) with L=m/r≥2L = m / r \ge 2, write xt=A[lo+tr]x_t = A[lo + t r] for t=0,…,L−1t = 0, \ldots, L - 1. This is the subsequence of the call. Its first half is x0,…,xL/2−1x_0, \ldots, x_{L/2 - 1} and its second half is xL/2,…,xL−1x_{L/2}, \ldots, x_{L-1}. Each comparator of the call has both wires in the subsequence, so the call changes no other entry of AA.

Lemma 1. Let y=0a1p−ay = 0^{a} 1^{p - a} be a sorted 0-1 sequence of even length pp. Then the items y0,y2,…,yp−2y_0, y_2, \ldots, y_{p-2} at even places hold ⌈a/2⌉\lceil a / 2 \rceil zeros and are sorted, and the items y1,y3,…,yp−1y_1, y_3, \ldots, y_{p-1} at odd places hold ⌊a/2⌋\lfloor a / 2 \rfloor zeros and are sorted.

Proof. A subsequence of a sorted sequence is sorted. The zeros are at places 0,…,a−10, \ldots, a - 1. Of these, ⌈a/2⌉\lceil a/2 \rceil are even and ⌊a/2⌋\lfloor a/2 \rfloor are odd. □\square

Lemma 2 (merge). If the first half and the second half of the subsequence of a call ODD-EVEN-MERGE(A,lo,m,r)(A, lo, m, r) are sorted 0-1 sequences, then after the call the subsequence is sorted.

Proof. By induction on L=m/rL = m / r, which is a power of 2.

If L=2L = 2, then s=2r=ms = 2r = m, so line L7 fails and line L11 compares x0x_0 and x1x_1. After it, x0≤x1x_0 \le x_1.

Let L≥4L \ge 4 and p=L/2p = L / 2, which is even. Let the first half have aa zeros and the second half bb zeros. The call of line L8 has the subsequence x0,x2,…,xL−2x_0, x_2, \ldots, x_{L-2} of stride ss. Its first half is the even places of the first half of xx, and its second half is the even places of the second half of xx, because pp is even. By Lemma 1 both are sorted. So by the induction hypothesis, after line L8 the even places of xx are sorted and hold E=⌈a/2⌉+⌈b/2⌉E = \lceil a/2 \rceil + \lceil b/2 \rceil zeros. In the same way, the call of line L9 has the subsequence x1,x3,…,xL−1x_1, x_3, \ldots, x_{L-1}, and after it the odd places of xx are sorted and hold O=⌊a/2⌋+⌊b/2⌋O = \lfloor a/2 \rfloor + \lfloor b/2 \rfloor zeros. The two calls have disjoint wires, so the order of lines L8 and L9 does not matter. Now x2t=0x_{2t} = 0 if and only if t<Et < E, and x2t+1=0x_{2t+1} = 0 if and only if t<Ot < O. Also E−O∈{0,1,2}E - O \in \{0, 1, 2\}.

  • If E=OE = O, the zeros are at the places 0,…,2E−10, \ldots, 2E - 1, so xx is sorted.
  • If E=O+1E = O + 1, the zeros are at the places 0,…,2E−20, \ldots, 2E - 2, so xx is sorted.
  • If E=O+2E = O + 2, the zeros are at the places 0,…,2E−40, \ldots, 2E - 4 and at the place 2E−22E - 2, and the place 2E−32E - 3 holds a 1. So xx is sorted except that x2E−3=1x_{2E - 3} = 1 and x2E−2=0x_{2E - 2} = 0. Here E≥2E \ge 2 and 2E−2≤L−22E - 2 \le L - 2, because the even places hold at most L/2L/2 zeros.

Line L10 runs over i=lo+r+2rti = lo + r + 2 r t for t=0,1,…t = 0, 1, \ldots while i+r<lo+mi + r < lo + m, and compares A[i]=x2t+1A[i] = x_{2t + 1} with A[i+r]=x2t+2A[i + r] = x_{2t + 2}. So it applies the comparators [2t+1:2t+2][2t + 1 : 2t + 2] of the subsequence for t=0,…,L/2−2t = 0, \ldots, L/2 - 2. These pairs are disjoint. In the first two cases each pair is already in order, and nothing changes. In the third case the pair (2E−3,2E−2)(2E - 3, 2E - 2) is one of them, with t=E−2t = E - 2, and its comparator exchanges the 1 and the 0. Each other pair is in order. After line L10, xx is sorted in each case. □\square

Theorem. For each power of 2 mm and each lolo, OEM-SORT(A,lo,m)(A, lo, m) sorts A[lo…lo+m−1]A[lo \ldots lo + m - 1] for each 0-1 content of this part, and it changes no other entry of AA. So by the zero-one principle, OEM-SORT(A,0,n)(A, 0, n) sorts each array of nn items from a totally ordered set.

Proof. By induction on mm. If m=1m = 1, line L1 returns. Let m≥2m \ge 2 and h=m/2h = m/2. By the induction hypothesis, lines L3 and L4 sort A[lo…lo+h−1]A[lo \ldots lo + h - 1] and A[lo+h…lo+m−1]A[lo + h \ldots lo + m - 1] and change nothing else. The call of line L5 has r=1r = 1, so its subsequence is A[lo…lo+m−1]A[lo \ldots lo + m - 1], and its two halves are sorted. By Lemma 2, after line L5 the part is sorted. ■\blacksquare

Back to Odd-even merge sort