Cocktail sort, also called directional bubble sort, is an improvement of the bubbling sort. The difference between this algorithm and the bubbling sort is from low to high and then high to low, while bubble sorting is only low to high to compare each element in the column.
It can get a little bit better sort than bubble sort.
void Cocktailsort (int *a, int n)
{
int left = 0;
int right = n-1;
while (left < right)
{
for (int i = left, I < right; i++)
{
if (A[i] > A[i+1])
Swap (a,i,i+1);
}
right--;
for (int i = right, I > left; i--)
{
if (A[i-1] > A[i])
Swap (a,i,i+1);
}
left++;
}
}
Analysis:
In the case of sequences (2,3,4,5,1), cocktail ordering requires only one sequence of accesses to be sorted, but it takes four times to use a bubbling sort, but in the state of the chaotic sequence,
Cocktail sequencing and bubble sorting are inefficient.
Bubble sort improvement: cocktail sort