Odd-Even Sort

Odd-even sort is an $$O(n^2)$$ algorithm related to Bubble Sort. It is stable and adaptive. It can be fully parallelized, giving $$O(n)$$ worst case time.

Odd-Even sort's parallel form is based on the concept of making it such that no indices overlap, so every compare can be done completely in parallel.

=Explanation= Odd-even sort makes passes through the array, similarly to Bubble Sort, but instead, it compares and swaps odd indices, then compares and swaps even indices, and repeats until no swaps are made. For 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 i = 0, 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 for i = 1, 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 while end