![]() |
VOOZH | about |
Cocktail Sort is a variation of Bubble sort. The Bubble sort algorithm always traverses elements from left and moves the largest element to its correct position in the first iteration and second-largest in the second iteration and so on. Cocktail Sort traverses through a given array in both directions alternatively. Cocktail sort does not go through the unnecessary iteration making it efficient for large arrays.
Cocktail sorts break down barriers that limit bubble sorts from being efficient enough on large arrays by not allowing them to go through unnecessary iterations on one specific region (or cluster) before moving onto another section of an array.
Algorithm:
Each iteration of the algorithm is broken up into 2 stages:
Example :
Let us consider an example array (5 1 4 2 8 0 2)
First Forward Pass:
(5 1 4 2 8 0 2) ? (1 5 4 2 8 0 2), Swap since 5 > 1
(1 5 4 2 8 0 2) ? (1 4 5 2 8 0 2), Swap since 5 > 4
(1 4 5 2 8 0 2) ? (1 4 2 5 8 0 2), Swap since 5 > 2
(1 4 2 5 8 0 2) ? (1 4 2 5 8 0 2)
(1 4 2 5 8 0 2) ? (1 4 2 5 0 8 2), Swap since 8 > 0
(1 4 2 5 0 8 2) ? (1 4 2 5 0 2 8), Swap since 8 > 2
After the first forward pass, the greatest element of the array will be present at the last index of the array.
First Backward Pass:
(1 4 2 5 0 2 8) ? (1 4 2 5 0 2 8)
(1 4 2 5 0 2 8) ? (1 4 2 0 5 2 8), Swap since 5 > 0
(1 4 2 0 5 2 8) ? (1 4 0 2 5 2 8), Swap since 2 > 0
(1 4 0 2 5 2 8) ? (1 0 4 2 5 2 8), Swap since 4 > 0
(1 0 4 2 5 2 8) ? (0 1 4 2 5 2 8), Swap since 1 > 0
After the first backward pass, the smallest element of the array will be present at the first index of the array.
Second Forward Pass:
(0 1 4 2 5 2 8) ? (0 1 4 2 5 2 8)
(0 1 4 2 5 2 8) ? (0 1 2 4 5 2 8), Swap since 4 > 2
(0 1 2 4 5 2 8) ? (0 1 2 4 5 2 8)
(0 1 2 4 5 2 8) ? (0 1 2 4 2 5 8), Swap since 5 > 2
Second Backward Pass:
(0 1 2 4 2 5 8) ? (0 1 2 2 4 5 8), Swap since 4 > 2
Now, the array is already sorted, but our algorithm doesn’t know if it is completed. The algorithm needs to complete this whole pass without any swap to know it is sorted.
(0 1 2 2 4 5 8) ? (0 1 2 2 4 5 8)
(0 1 2 2 4 5 8) ? (0 1 2 2 4 5 8)
Below is the implementation of the above algorithm :
Sorted array : 0 1 2 2 4 5 8
People have given many different names to cocktail sort:
| Case | Complexity |
| Best Case | O(n) |
| Average Case | O(n2) |
| Worst Case | O(n2) |
| Space | O(1) Auxiliary Space |
| Maximum number of Comparison | O(n2) |
Sorting In Place: Yes
Stable: Yes
Comparison with Bubble Sort:
Time complexities are the same, but Cocktail performs better than Bubble Sort. Typically cocktail sort is less than two times faster than bubble sort. Consider the example (2, 3, 4, 5, 1). Bubble sort requires four traversals of an array for this example, while Cocktail sort requires only two traversals. (Source Wiki)
| Number of Elements | Unoptimized Bubble Sort | Optimized Bubble sort | Cocktail sort |
| 100 | 2ms | 1ms | 1ms |
| 1000 | 8ms | 6ms | 1ms |
| 10000 | 402ms | 383ms | 1ms |
Cocktail sort, also known as cocktail shaker sort or bidirectional bubble sort, is a variation of the bubble sort algorithm. Like the bubble sort algorithm, cocktail sort sorts an array of elements by repeatedly swapping adjacent elements if they are in the wrong order. However, cocktail sort also moves in the opposite direction after each pass through the array, making it more efficient in certain cases.
Sure, here are some potential advantages and disadvantages of using the cocktail sort algorithm:
[2, 3, 5, 6, 7, 9]
Complexity Analysis :
The time complexity of Cocktail Sort is O(n^2) in the worst and average cases, where n is the number of elements in the array. This is because the algorithm iterates through the array multiple times, and in the worst case, each iteration involves comparing every element to its neighbor and possibly swapping them.
The best case time complexity is O(n) when the array is already sorted, but this is a rare case.
The space complexity of Cocktail Sort is O(1), which is constant. This is because the algorithm sorts the array in place, without using any extra memory.
References: