The odd-even mergesort algorithm was developed by K.E. Batcher [Bat 68]. It is based on a merge algorithm that merges two sorted halves of a sequence to a completely sorted sequence.
In contrast to mergesort, this algorithm is not data-dependent, i.e. the same comparisons are performed regardless of the actual data. Therefore, odd-even mergesort can be implemented as a sorting network.
The following algorithm merges a sequence whose two halves are sorted to a sorted sequence.
Algorithm odd-even merge(n)
Input:
sequence a0, ..., an-1 of length n > 1 whose two halves a0, ..., an/2-1 and an/2, ..., an-1 are sorted (n a power of 2)
Output:
the sorted sequence
Method:
The correctness of the merge algorithm is proved using induction and the 0-1-principle.
If n = 21 the sequence is sorted by the comparison [0:1]. So let n = 2k, k > 1 and assume the algorithm is correct for all smaller k (induction hypothesis).
Consider the 0-1-sequence a = a0, ..., an-1 to be arranged in rows of an array with two columns. The corresponding mapping of the index positions is shown in Figure 1a, here for n = 16. Then Figure 1b shows a possible situation with a 0-1-sequence. Each of its two sorted halves starts with some 0's (white) and ends with some 1's (gray).
Figure 1: Situations during execution of odd-even merge
In the left column the even subsequence is found, i.e. all ai with i even, namely a0, a2, a4 etc.; in the right column the odd subsequence is found, i.e. all ai with i odd, namely a1, a3, a5 etc. Just like the original sequence the even as well as the odd subsequence consists of two sorted halves.
By induction hypothesis, the left and the right column are sorted by recursive application of odd-even merge(n/2) in step 1 of the algorithm. The right column can have at most two more 1's than the left column (Figure 1c).
After performing the comparisons of step 2 of the algorithm (Figure 1d), in each case the array is sorted (Figure 1e).
Let T(n) be the number of comparisons performed by odd-even merge(n). Then we have for n > 2
T(n) = 2·T(n/2) + n/2-1.
With T(2) = 1 we have
T(n) = n/2 · (log(n)-1) + 1 ∈ O(n·log(n)).
By recursive application of the merge algorithm the sorting algorithm odd-even mergesort is formed.
Algorithm odd-even mergesort(n)
Input:
sequence a0, ..., an-1 (n a power of 2)
Output:
the sorted sequence
Method:
Figure 2 shows the odd-even mergesort network for n = 8.
Figure 2: Odd-even mergesort for n = 8
The number of comparators of odd-even mergesort is in O(n log(n)2).
An implementation of odd-even mergesort in Java is given in the following. The algorithm is encapsulated in a class OddEvenMergeSorter. Its method sort passes the array to be sorted to array a and calls function oddEvenMergeSort.
Function oddEvenMergeSort recursively sorts the two halves of the array. Then it merges the two halves with oddEvenMerge.
Function oddEvenMerge picks every 2r-th element starting from position lo and lo+r, respectively, thus forming the even and the odd subsequence. According to the recursion depth r is 1, 2, 4, 8, ....
With the statements
an object of type OddEvenMergeSorter is created and its method sort is called in order to sort array b. The length n of the array must be a power of 2.
There are other sorting networks that have a complexity of O(n log(n)2), too, e.g. bitonic sort and shellsort. However, odd-even mergesort requires the fewest comparators of these. The following table shows the number of comparators for n = 4, 16, 64, 256 and 1024.
n | odd-even mergesort | bitonic sort | shellsort |
---|---|---|---|
4 | 5 | 6 | 6 |
16 | 63 | 80 | 83 |
64 | 543 | 672 | 724 |
256 | 3839 | 4608 | 5106 |
1024 | 24063 | 28160 | 31915 |
Exercise 1: Give the exact formula for T(n), the number of comparators of odd-even mergesort. Check your formula by comparing its results with the entries in the table above.
[Bat 68] K.E. Batcher: Sorting Networks and their Applications. Proc. AFIPS Spring Joint Comput. Conf., Vol. 32, 307-314 (1968)
[Sed 03] R. Sedgewick: Algorithms in Java, Parts 1-4. 3rd edition, Addison-Wesley (2003)
Next: [Bitonic sort] or [up]