Class: | Sorting algorithm |
Data: | Array |
Best-Time: | O(log2(n)) |
Average-Time: | O(log2(n)) |
Time: | O(log2(n)) |
Space: | O(nlog2(n)) |
Optimal: | No |
Batcher's odd–even mergesortis a generic construction devised by Ken Batcher for sorting networks of size O(n (log n)2) and depth O((log n)2), where n is the number of items to be sorted. Although it is not asymptotically optimal, Knuth concluded in 1998, with respect to the AKS network that "Batcher's method is much better, unless n exceeds the total memory capacity of all computers on earth!"[1]
It is popularized by the second GPU Gems book,[2] as an easy way of doing reasonably efficient sorts on graphics-processing hardware.
Various recursive and iterative schemes are possible to calculate the indices of the elements to be compared and sorted. This is one iterative technique to generate the indices for sorting n elements:
for p = 1, 2, 4, 8, ... # as long as p < n for k = p, p/2, p/4, p/8, ... # as long as k >= 1 for j = mod(k,p) to (n-1-k) with a step size of 2k for i = 0 to min(k-1, n-j-k-1) with a step size of 1 if floor((i+j) / (p*2))
Non-recursive calculation of the partner node index is also possible.[3]