User:Fungamer2/sandbox

Odd-even sort is a $$O(n^2)$$ algorithm related to Bubble Sort. It is stable, but it is not adaptive.

=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 sorted. 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!