Odd-Even Sort

Odd-even Sort is an $$O(n^2)$$ algorithm related to Bubble Sort. It is stable and adaptive. Odd-Even arranges its comparators in such a way so that they don't overlap each other for each layer. There are exactly $$n$$ layers with $$O(n)$$ comparators each therefore, when fully parallelized, each layer can be done in $$O(1)$$ yielding an $$O(n)$$ worst case parallel time.

Odd-Even Sort makes passes through the array, similarly to Bubble Sort, compare-and-swapping odd pairs of indices: $$(0,1),(2,3),\cdots$$ then even pairs: $$(1,2),(3,4),\cdots$$. It repeats for $$n$$ iterations and can terminate early after no swaps were made on two passes. Adding this early termination makes Odd-Even's running time $$O(kn)$$ where $$k$$ is the maximum distance of an inversion.

Example
Odd-Even sort random array: 3 4 5 1 2 Compare and swap odd indices: 3 4 5 1 2  3 4 1 5 2 └─┘ └─┘         └─┘ Compare and swap even indices: 3 4 1 5 2  3 1 4 2 5   └─┘ └─┘     └─┘ └─┘ Repeat until no swaps were made or after n iterations: 3 1 4 2 5  1 3 2 4 5 └─┘ └─┘     └─┘ └─┘ 1 3 2 4 5   1 2 3 4 5   └─┘ └─┘     └─┘ 1 2 3 4 5   1 2 3 4 5   <- since no swaps were made, the data is sorted └─┘ └─┘                   (this is also the nth iteration so the data would be sorted even if swaps were made)

Pseudocode
procedure odd_even_sort(array arr) do bool sorted = false; while not sorted do sorted = true; for j = 0, j < 2, j += 1 do for i = j, i < length(arr), i += 2 do if arr[i] > arr[i + 1] do swap arr[i] and arr[i + 1] sorted = false; end if end for end for end while end