![]() |
VOOZH | about |
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases.
In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even indexed elements.
Output :
-9 2 10 34
We demonstrate the above algorithm using the below illustration on the array = {3, 2, 3, 8, 5, 6, 4, 1}
Please refer wiki for proof of correctness.
Time Complexity : O(N2) where, N = Number of elements in the input array.
Auxiliary Space : O(1). Just like bubble sort this is also an in-place algorithm.
Exercise
In our program in each iteration we first do bubble sort on odd indexed elements and then a bubble sort on the even indexed elements.
Will we get a sorted result if we first perform a bubble sort on even indexed element first and then on the odd indexed element ?
References
https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort