Cocktail sort is a modification of bubble sort. It is also known as bidirectional bubble sort, cocktail shaker sort, shaker sort, shuffle sort, shuttle sort, happy hour sort, or ripple sort. It works in sorting elements in both directions. The process of sorting starts by shifting the largest element to the rightmost side (like bubble sort). After that, the algorithm moves in the opposite direction and shifts the smallest element to the leftmost side. The smallest and largest elements are placed at their final positions in the first iteration. These steps are continued on the unsorted array until the entire array is sorted.
Play the following slides to visualize the working of the cocktail sort:
Note: The cocktail sort algorithm, similar to other variants of bubble sort, is used as an educational tool. More efficient sorting algorithms, i.e., merge sort, quick sort, or timsort, are preferred in real-world applications. Most programming languages use these algorithms for their built-in sorting algorithms. For example, Python uses timsort in its built-in
sorted()
function and.sort()
method.
Let’s discuss the sequence of the steps for the algorithm:
swapped := Truestart := 0end := size of array - 1while swapped is Trueswapped := Falsefor i from start to endif array[i] > array[i+1]swap both valuesswapped := Trueend := end-1if swapped is Falsebreak the loopswapped := Falsefor i from end to startif array[i] < array[i-1]swap both valuesswapped := Truestart := start+1return array
The cocktail sort is a bit different from the bubble sort. Let’s discuss them below:
The cocktail sort should be used instead of bubble sort in the following scenarios:
Let’s implement the code of cocktail sort in the following playground:
def cocktail_sort(arr:list):swapped = Truestart, end = 0, len(arr)-1while swapped:swapped = False # Reset the flag for the forward passfor i in range(start, end):if arr[i] > arr[i+1]: # Compare and shift the larger element to rightarr[i], arr[i+1] = arr[i+1], arr[i]swapped = Trueend = end - 1if not swapped: break # Break the loop if no swapping happensswapped = False # Reset the flag for the backward passfor i in range(end, start, -1):if arr[i] < arr[i-1]: # Compare and shift the smaller element to leftarr[i], arr[i-1] = arr[i-1], arr[i]swapped = Truestart = start + 1return arrif __name__ == "__main__":unsorted_arr = [15, 13, 24, 7, 18, 3, 22, 9]print("Unsorted array:", unsorted_arr)print("Sorted array:", cocktail_sort(unsorted_arr))
Let’s discuss the above code:
swapped
, start
, and end
to store of the indices.while
loop to implement the sorting in which:
swapped
flag to False
and used the for
loop for the forward pass. In the forward pass, we do element-wise swapping if required and set the swapped
flag to True
. At the end of the for
loop, the end
index is shifted to the previous index.while
loop if no swapping occurs in the forward pass.swapped
flag to False
and used the for
loop for the backward pass. In the backward pass, we do element-wise swapping if required and set the swapped
flag to True
. At the end of the for
loop, the start
index is shifted to the next index.cocktail_sort()
function. We use print
statements to show the array before and after sorting.The worst-case and average-case time complexity of the cocktail shaker algorithm is .
However, when the list is almost sorted before using the sorting algorithm, its complexity approaches . For example, if each element is at most positions away from its final position, the complexity of the cocktail sort will be .
In the array, , there are two elements away from their final positions. It required only one iteration of cocktail sort for shifting and to their actual positions.
The space complexity of the cocktail shaker is .
Let’s discuss some pros and cons of the cocktail sort.
Unlock your potential: Sorting algorithms series – all in one place!
To continue your exploration of sorting algorithms, check out our series of Answers below:
What are sorting algorithms?
Understand the fundamentals of sorting algorithms and their role in organizing data efficiently.
What is tree sort?
Learn how tree-based structures can be used to sort elements efficiently.
What is Bitonic sort?
Discover Bitonic Sort, a parallel sorting algorithm ideal for hardware implementations.
What is Flash Sort?
Explore Flash Sort, a hybrid sorting technique designed for fast performance on large datasets.
How to implement the pigeonhole sorting algorithm in Python
A step-by-step guide to implementing Pigeonhole Sort in Python for efficient data sorting.
How to implement comb sort in C++
Learn how to implement Comb Sort in C++ to improve sorting efficiency over Bubble Sort.
Implementation of the cocktail sort in C++
Understand how Cocktail Sort refines Bubble Sort for bidirectional sorting in C++.
How to implement cocktail sort in Python
Implement Cocktail Sort in Python to enhance sorting performance with a two-way pass.
How to implement Gnome sort in C++
Explore the simplicity of Gnome Sort and its implementation in C++.
How to implement pancake sort in Java?
Learn how to implement Pancake Sort in Java, inspired by flipping pancakes to sort stacks.
Comparison of linear-time sorting algorithms
Analyze and compare different linear-time sorting algorithms to understand their efficiency.
Free Resources