Odd-Even Sort

Odd-even sort is an $$O(n^2)$$ algorithm related to Bubble Sort. It is stable, and it is 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:

3 4 5 1 2

Compare and swap odd indices:

Compare indices 0 and 1. They are in order, so leave them.

3 4 5 1 2

Compare indices 2 and 3. They are out of order, so swap them.

3 4 1 5 2

Compare and swap even indices:

Compare indices 1 and 2. They are out of order, so swap them.

3 1 4 5 2

Compare indices 3 and 4. They are out of order, so swap them.

3 1 4 2 5

Not sorted, so repeat.

Compare and swap odd indices:

Compare indices 0 and 1. They are out of order, so swap them.

1 3 4 2 5

Compare indices 2 and 3. They are out of order, so swap them.

1 3 2 4 5

Compare and swap even indices:

Compare indices 1 and 2. They are out of order, so swap them.

1 2 3 4 5

Compare indices 3 and 4. They are in order, so leave them.

1 2 3 4 5

Sorted!

=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