Odd-Even bubble sort is a variation of the bubble sort algorithm, that is adapted to take advantage for multiprocessing environments, by tasking individual processors to perform comparisons and swaps due to its inherent non-overlapping nature.
In a Odd-Even bubble sort, all elements in odd/even positions are compared with the element in position next to them. If it is found that the element to the right of the odd/even position is greater than element at the odd/even position, then they are swapped.
So the steps include:
Step 1:
Compare element in next Odd position with the next element and perform swap if necessary.
Step 2:
Repeat Step 1, until we reach the last element in position of the data-set
Now, these steps 1 & 2 are repeated for the elements in Even positions.
Now by performing these steps 1 & 2 for all elements at odd positions, followed by all elements at even positions, only moves the bigger element by 2 positions to the right at the most, with one-position shift during step 1 while sorting Odd positions, and that is followed by one-position shift during step 1 while sorting Even positions.
Obviously a 2 position shift does not guarantee that the biggest element will necessarily move to the end of the data-set, provided the biggest element is at position 0, and the data-set is larger than 3. It needs to be moved further, to definitively shift to the end of the data-set which means that steps 1 & 2, for odd and even positions need to be repeated.
In order to determine, how many times steps 1 & 2, for odd and even positions need to be repeated, let us reexamine the fact that the largest element gets shifted 2 positions at the most by performing steps 1 & 2, for odd and even positions. For a element at position 0, to be shifted to position n-1 in a n element data-set, we can easily deduce that n-1 shifts need to happen such as 0->1,1->2,......,n-2->n-1.
An example often helps understand concepts and here is one, for you.
In a 5 element data-set, for a big element to travel from position 0 to position 4, the following 4 shifts need to be happen:
In a 5 element data-set, for a big element to travel from position 0 to position 4, the following 4 shifts need to be happen:
Shift 1: Position 0 -> Position 1
Shift 2: Position 1 -> Position 2
Shift 3: Position 2 -> Position 3
Shift 4: Position 3 -> Position 4
Now, we already concluded that a big element travels 2 positions at most with repetition of steps 1 & 2, for odd and even positions. So for a big element at position 0 to travel to position 4 in a 5 element data-set, at the rate of 2 positions shifts per repetition of steps 1 & 2, we only need:
4 positions to shift/2 positions shift per repetition
= 2 repetitions of odd and even shift steps 1 & 2
= 2 repetitions of odd and even shift steps 1 & 2
And the shifts would look like this:
Step 1 - Odd: Shift 1: Position 0 -> Position 1
Step 1 - Even: Shift 1: Position 1 -> Position 2
Step 1 - Odd: Shift : Position 2 -> Position 3
Step 1 - Even: Shift 1: Position 3 -> Position 4
So for a element to shift from 0 to n-1 in a n element data-set, at the rate of 2 positions shifts per repetition of steps 1 & 2, we only need:
n-1 positions to shift/2 positions shift per repetition
= (n-1)/2 repetitions of odd and even shift steps 1 & 2
which we will refer as “repetition count” here after.
Since there is no such thing as half-shift in our algorithm, we always “ceil” the result of the (n-1)/2 to the nearest higher integer, so a 5/2 = 2.5 becomes 3, in a 6 element data-set.
The following video demonstrates how a Odd-Even algorithm works, since nothing can beat seeing something in action:
In our programmatic implementation, we use the following variables to represent the key variables to track the odd, even and repetition count:
Next odd position to sort ("Odd" pointer in workings video): "op"
Next even position to sort("Even" pointer in workings video): "ep"
Repetition count("Repetition Counter" in workings video): "rc"
And we utilize the “ceil” method that is defined in several languages to determine the value of “rc”. Here is a reference to java’s own “ceil” method: