Odd-even sort

O(n²)

Odd-even sort (a parallel-friendly bubble sort) alternates between comparing odd-indexed adjacent pairs (1-2, 3-4, …) and even-indexed pairs (0-1, 2-3, …), swapping any that are out of order. It repeats until a full round makes no swap. The fixed, independent pairs make it a natural fit for parallel hardware, though it is still O(n²).

Array
Edit the input and press Play

How it works

  1. Compare each odd-indexed pair (positions 1-2, 3-4, …) and swap if out of order.
  2. Compare each even-indexed pair (positions 0-1, 2-3, …) and swap if out of order.
  3. Each round can run all its pair comparisons independently — handy in parallel.
  4. Repeat until a full round makes no swaps.

Pseudocode

1oddEvenSort(a):                     # O(n²)2  sorted ← false3  while not sorted:4    sorted ← true5    for i = 1, 3, 5, … while i+1 < n:   # odd pairs6      if a[i] > a[i+1]: swap; sorted ← false7    for i = 0, 2, 4, … while i+1 < n:   # even pairs8      if a[i] > a[i+1]: swap; sorted ← false