Algorithm Visualiser by Tsvetozar Biserov

Odd-Even Sort

  • Ω( n )
  • Θ( n2 )
  • O( n2 )
  • space O( 1 )

Odd-even sort, also called odd-even transposition sort, works in rounds of two phases. The first phase compares the pairs that start at an odd index, (1,2), (3,4) and so on, and the second compares the pairs that start at an even index, (0,1), (2,3) and so on. No two pairs in a phase share a cell, so they could run at once. The sort ends after a round in which nothing swapped.

Array
Comparator strip Index down, time to the right
100 elements
×20 steps/frame

Comparisons 1
—
Reads 2
—
Writes 2
—
Swaps 3
—
Real time 4
—
Visual time 4
—
  1. Comparisons count only element-against-element checks; loop and index checks are never counted.
  2. Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
  3. A swap counts once, as a swap. It is never also counted as a read or a write.
  4. Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.

How odd-even sort works

In the first round the odd phase looks at (1,2), holding 2 and 6, and (3,4), holding 1 and 4. Both pairs are in order, so nothing moves. The even phase then swaps all three of its pairs one after another: 5 and 2 give [2, 5, 6, 1, 4, 3], 6 and 1 give [2, 5, 1, 6, 4, 3] and 4 and 3 give [2, 5, 1, 6, 3, 4]. A phase that finds nothing to do is normal, and it does not mean the array is sorted.

In round two the odd phase swaps 5 with 1 to give [2, 1, 5, 6, 3, 4], and 6 with 3 to give [2, 1, 5, 3, 6, 4]. The even phase swaps 2 with 1, then 5 with 3, then 6 with 4, passing through [1, 2, 5, 3, 6, 4] and [1, 2, 3, 5, 6, 4] to reach [1, 2, 3, 5, 4, 6]. Large values drift right and small ones drift left at the same time, which is the effect bubble sort needs two directions to achieve.

Round three is short on work. The odd phase swaps 5 and 4 at indexes 3 and 4 and the array is [1, 2, 3, 4, 5, 6], and the even phase finds nothing. A fourth round compares all five pairs again, swaps none and ends the run. Each round makes five comparisons, two in the odd phase and three in the even one, so four rounds cost 20 comparisons for 9 swaps.

Odd-Even Sort on 5 2 6 1 4 3, one swap or write at a time. Indexes count from 0.
Step Change Array after
1 Swap indexes 0 and 1 2 5 6 1 4 3
2 Swap indexes 2 and 3 2 5 1 6 4 3
3 Swap indexes 4 and 5 2 5 1 6 3 4
4 Swap indexes 1 and 2 2 1 5 6 3 4
5 Swap indexes 3 and 4 2 1 5 3 6 4
6 Swap indexes 0 and 1 1 2 5 3 6 4
7 Swap indexes 2 and 3 1 2 3 5 6 4
8 Swap indexes 4 and 5 1 2 3 5 4 6
9 Swap indexes 3 and 4 1 2 3 4 5 6

All 9 changes are shown.

Pseudocode

sorted = false
while not sorted
  sorted = true
  for start in [1, 0]
    for i = start, start + 2, ... while i + 1 < n
      if a[i + 1] < a[i]
        swap a[i], a[i + 1]
        sorted = false

Why these complexities

A round on n elements makes n-1 comparisons, split between the two phases. Sorted input finishes after a single round of n-1 comparisons with no swaps, which is the linear best case and the reason the sort counts as adaptive. A round with a swap forces another round, because the flag is checked only after both phases.

At most n phases, about n/2 rounds, do any work. The usual proof uses the 0-1 principle: if the algorithm sorts every array of zeros and ones, it sorts every array, and for those the ones move right at one step per phase after a short delay, which fits within n phases. With the confirming round the worst case is about n²/2 comparisons, close to bubble sort's n(n-1)/2, and it is quadratic. The number of swaps is the number of inversions, since each swap fixes exactly one, which averages n(n-1)/4.

The sequential cost is not what the algorithm is for. Each phase is a set of independent compare-exchange operations, so n/2 processors can run a phase in constant time, and n phases then take O(n) time with O(n²) total work. The same structure is a sorting network of n columns of comparators. Memory is constant here: the only state is a flag and two loop indexes.

Measured on this site

Exact counts from this site's own engine for 32 elements, using the same rules as the visualiser.

Input Comparisons Writes Swaps
Random 527 0 311
Already sorted 31 0 0
Reversed 527 0 496
Few distinct values 403 0 169

When to use it

Use it when the comparisons of a phase can run in parallel, for example on a processor array or a GPU. On a single processor it offers nothing over bubble sort.

Frequently asked questions

Why does it have odd and even phases?

Pairs inside one phase do not overlap, so all their compare-and-swaps are independent and can run at the same time.

How many rounds does it need?

At most n rounds. After that many rounds the array is sorted, and the run stops earlier when a full round makes no swap.

Is odd-even sort stable?

Yes. It only swaps adjacent elements that are strictly out of order.

Why does a round without swaps prove that the array is sorted?

Both phases of the round found every pair in order. The odd phase checked the pairs (1,2), (3,4), and the even phase checked (0,1), (2,3), so together they covered every neighbouring pair, and no inversion between neighbours means none at all. One clean phase is not enough: in the trace the odd phase of round one finds nothing while the array is far from sorted.

How much faster would odd-even sort be on parallel hardware?

With about n/2 comparators working at the same time, each phase takes one step, and n phases always suffice, so it finishes in about n steps, linear time instead of quadratic. The number of comparisons does not shrink, only the time. Algorithms with a logarithmic number of parallel steps, such as Batcher's merge sort network, beat it on large inputs, and it wins when only neighbouring cells can communicate.

Background

Nico Habermann described the technique in 1972 in a report on parallel neighbour sorting, written for a machine with a row of processors that can talk only to the cell on each side. Knuth presents it in his treatment of sorting networks, where it appears as a network with n columns of comparators, and it is also known as odd-even transposition sort.

Its main use has been in teaching parallel algorithms and on linear arrays of processing elements, where it is often the base case for sorting on meshes. It is a different algorithm from Batcher's odd-even merge sort, which has a similar name but a recursive structure and sorts in O(log² n) parallel steps.

References

  • Knuth, The Art of Computer Programming, vol. 3, section 5.3.4 (Networks for Sorting)
  • Habermann, Parallel Neighbor-Sort (or the Glory of the Induction Principle), Carnegie-Mellon University technical report, 1972
  • Leighton, Introduction to Parallel Algorithms and Architectures: Arrays, Trees, Hypercubes, Morgan Kaufmann, 1992, chapter 1

Compare with

Algorithm Average time Worst time
Bubble Sort Θ( n2 ) O( n2 )
Cocktail Shaker Sort Θ( n2 ) O( n2 )
Comb Sort Θ( n2 ) O( n2 )
Gnome Sort Θ( n2 ) O( n2 )
Quick Sort Θ( n log n ) O( n2 )
3-Way Quick Sort Θ( n log n ) O( n2 )
Dual-Pivot Quick Sort Θ( n log n ) O( n2 )
Big O notation
Best time Ω( n ) good
Average time Θ( n2 ) poor
Worst time O( n2 ) poor
Worst space O( 1 ) good

Properties

Stable
Yes
In place
Yes
Adaptive
Yes
Comparison-based
Yes