Cocktail Sort Algorithm

In this article, we will discuss the cocktail sort algorithm. Cocktail sort is the variation of Bubble Sort, which traverses the list in both directions alternatively. It is different from bubble sort in the sense that bubble sort traverses the list in the forward direction only, while this algorithm traverses in forward as well as backward direction in one iteration.

Cocktail sort is also called as bi-directional bubble sort. In bubble sort, elements are traversed from left to right, i.e., in one direction. In first iteration, bubble sort first moves the largest element to its correct position, then the second-largest element in its exact position, and so on. But cocktail sort traverses in both directions alternatively.

As similar to the bubble sort, the worst and average case complexities of cocktail sort is O(n2). Cocktail sort is faster than bubble sort.

There are two stages of cocktail sort in one iteration that are listed as follows -

  1. In first stage, similar to the bubble sort, loop through the array from left to right. The adjacent elements are compared, and if the left element is greater than the right one, we swap those elements. The largest element of the list is placed at the end of the array in the forward pass.
  2. In second stage, loop through the array from the rightmost unsorted element to the left. The adjacent elements are compared, and if the right element is smaller than the left element then, we swap those elements. The smallest element of the list is placed at the beginning of the array in the backward pass.

This process continues until the array elements are not sorted.

Now, let's see the algorithm of cocktail sort algorithm.

Algorithm

Working of Cocktail Sort Algorithm

Now, let's see the working of the cocktail sort Algorithm.

To understand the working of the cocktail sort algorithm, let's take an unsorted array. We are taking a short and accurate array, as we know the complexity of the cocktail sort is O(n2).

Let the elements of array are -

Array = {4, 0, 3, 1, 7, 1, 2}

Iteration 1:

First Forward pass:

In first iteration, the forward pass is similar to the bubble sort. The comparisons of forward pass in first iteration are given as follows -

Sorting will start from the initial two elements. Let compare them to check which is greater.

Cocktail Sort Algorithm

After swapping new array will look like -

Cocktail Sort Algorithm

Now compare 4 and 3.

Cocktail Sort Algorithm

After swapping new array will look like -

Cocktail Sort Algorithm

Now, compare 4 and 1.

Cocktail Sort Algorithm

After swapping new array will look like -

Cocktail Sort Algorithm

Now, compare 4 and 7.

Cocktail Sort Algorithm

Now, the comparison will be between 7 and 1.

Cocktail Sort Algorithm

After swapping new array will look like -

Cocktail Sort Algorithm

Now, compare 7 and 2.

Cocktail Sort Algorithm

Now, we reach at the end of the array. After swapping and first forward pass, the array elements will look like -

Cocktail Sort Algorithm

After the first forward pass, the largest element of the array is stored at the last position of the array.

First Backward pass:

Now, the first backward pass is started. It will be started from the last index of the array except for the index where the largest element is stored.

So, from the backward direction, first array elements 2 and 1 will be compared.

Cocktail Sort Algorithm

Now compare, 1 and 4.

Cocktail Sort Algorithm

After swapping, the array will be -

Cocktail Sort Algorithm

Now compare 1 and 1.

Cocktail Sort Algorithm

Now compare, 3 and 1.

Cocktail Sort Algorithm

After swapping, the array will be -

Cocktail Sort Algorithm

Now compare, 0 and 1.

Cocktail Sort Algorithm

So, after the first backward pass, smallest element from the array is stored at the first index of array. So, after first iteration, the array elements will be -

Cocktail Sort Algorithm

Iteration 2:

Second Forward pass:

Now, the second forward pass is started. It will be started from the first index of the array except for the index where the smallest element is stored.

So, from the forward direction, first array elements 1 and 3 will be compared.

Cocktail Sort Algorithm
Cocktail Sort Algorithm
Cocktail Sort Algorithm
Cocktail Sort Algorithm

Now, we reach at the end of the array. After the second forward pass, the second largest array element will be stored at its exact position. After swapping and second forward pass, the array elements will look like -

Cocktail Sort Algorithm

Second Backward pass:

Now, the second backward pass is started.

So, from backward direction, array elements 3 and 2 will be compared.

Cocktail Sort Algorithm

After swapping, the array will be -

Cocktail Sort Algorithm

Now, the array is completely sorted, but the algorithm has to complete the entire pass without any swap to know that the array is sorted.

Cocktail Sort Algorithm
Cocktail Sort Algorithm

So, after sorting the array elements will be -

Cocktail Sort Algorithm

Now, the array is completely sorted.

Now, the array is completely sorted.

Cocktail sort complexity

Now, let's see the time complexity of cocktail sort in the best case, average case, and worst case. We will also see the space complexity of the cocktail sort.

1. Time Complexity

CaseTime Complexity
Best CasedO(n)

Average CaseO(n2)
Worst CaseO(n2)
  • Best Case Complexity - It occurs when there is no sorting required, i.e., the array is already sorted. The best-case time complexity of cocktail sort is O(n).
  • Average Case Complexity - It occurs when the array elements are in jumbled order that is not properly ascending and not properly descending. The average case time complexity of cocktail sort is O(n2).
  • Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse order. That means suppose you have to sort the array elements in ascending order, but its elements are in descending order. The worst-case time complexity of cocktail sort is O(n2).

2. Space Complexity

Space ComplexityO(1)
StableYES
  • The space complexity of the cocktail sort is O(1).

Implementation of cocktail sort

Now, let's see the programs of cocktail sort in different programming languages.

Program: Write a program to implement cocktail sort in C language.

Output:

Cocktail Sort Algorithm

Program: Write a program to implement cocktail sort in C++.

Output:

Cocktail Sort Algorithm

Program: Write a program to implement cocktail sort in C#.

Output:

Cocktail Sort Algorithm

Program: Write a program to implement cocktail sort in Java.

Output:

Cocktail Sort Algorithm

So, that's all about the article. Hope the article will be helpful and informative to you.


Next TopicCycle Sort




Latest Courses