Odd-Even Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Odd-Even Sort
A highly parallelizable exchange sort: alternates between swapping adjacent odd-indexed pairs and even-indexed pairs, forming the basis for hardware-based sorting networks.
How it works
Odd-Even Sort (originally developed for parallel processors with local interconnections) is a variation of Bubble Sort. Standard Bubble Sort is strictly sequential because each comparison depends on the result of the previous one. Odd-Even Sort separates comparisons into two alternating, independent sweeps:
- Odd Phase: Compare all adjacent element pairs starting at odd indices: \((A[1], A[2]), (A[3], A[4]), (A[5], A[6]), \dots\). Swap any out-of-order pairs. Because these pairs do not overlap, all comparisons in this phase can run concurrently.
- Even Phase: Compare all adjacent element pairs starting at even indices: \((A[0], A[1]), (A[2], A[3]), (A[4], A[5]), \dots\). Swap any out-of-order pairs. Again, these comparisons can run concurrently.
- Repeat: Alternate between Odd and Even phases until a full cycle of both phases occurs without any swaps.
Complexity
Interactive visualization
Pseudocode
procedure oddEvenSort(A): n ← length(A) sorted ← false while not sorted: sorted ← true # Odd Phase for i from 1 to n - 2 step 2: if A[i] > A[i + 1]: swap A[i], A[i + 1] sorted ← false # Even Phase for i from 0 to n - 2 step 2: if A[i] > A[i + 1]: swap A[i], A[i + 1] sorted ← false
JavaScript implementation
function oddEvenSort(arr) { const n = arr.length; let sorted = false; while (!sorted) { sorted = true; // Odd phase for (let i = 1; i < n - 1; i += 2) { if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; sorted = false; } } // Even phase for (let i = 0; i < n - 1; i += 2) { if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; sorted = false; } } } return arr; }
Pros and cons
Pros
- Inherently parallelizable, suited for hardware architectures.
- In-place: operates directly in O(1) extra memory.
- Stable: identical items preserve original order.
Cons
- On standard single-core CPUs, it is slower than traditional Bubble Sort.
- Average and worst-case complexity are still quadratic \(O(n²)\).
- Performs many redundant sweeps on randomized arrays.
When (not) to use it
- Multi-processor hardware: Ideal for hardware circuits, FPGA routing, and parallel computing clusters.
- Standard software: Avoid for CPU-bound single-thread code; Insertion Sort or Quicksort are much faster.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid