Odd-Even Sort

Alternates between odd-indexed and even-indexed passes, comparing and swapping adjacent pairs. Originally designed for parallel processors.

Best O(n) Avg O(n²) Worst O(n²) Space O(1) Stable Yes In-place Yes Comparison-based

How it works

Alternates between odd-indexed and even-indexed passes, comparing and swapping adjacent pairs. Originally designed for parallel processors.

Implementation

function oddEvenSort(arr) {
  let sorted = false;
  while (!sorted) {
    sorted = true;
    for (let i = 1; i < arr.length-1; i += 2)
      if (arr[i] > arr[i+1]) {
        [arr[i], arr[i+1]] = [arr[i+1], arr[i]];
        sorted = false;
      }
    for (let i = 0; i < arr.length-1; i += 2)
      if (arr[i] > arr[i+1]) {
        [arr[i], arr[i+1]] = [arr[i+1], arr[i]];
        sorted = false;
      }
  }
}