## Cocktail Sort AlgorithmIn 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 As similar to the bubble sort, the worst and average case complexities of cocktail sort is There are two stages of cocktail sort in one iteration that are listed as follows - - 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.
- 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 AlgorithmNow, 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 Let the elements of array are -
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. After swapping new array will look like - Now compare 4 and 3. After swapping new array will look like - Now, compare 4 and 1. After swapping new array will look like - Now, compare 4 and 7. Now, the comparison will be between 7 and 1. After swapping new array will look like - Now, compare 7 and 2. Now, we reach at the end of the array. After swapping and first forward pass, the array elements will look like - After the first forward pass, the largest element of the array is stored at the last position of the array.
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 Now compare, 1 and 4. After swapping, the array will be - Now compare 1 and 1. Now compare, 3 and 1. After swapping, the array will be - Now compare, 0 and 1. 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 -
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 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 -
Now, the second backward pass is started. So, from backward direction, array elements After swapping, the array will be - Now, the array is completely sorted, but the algorithm has to complete the entire pass without any So, after sorting the array elements will be - Now, the array is completely sorted. Now, the array is completely sorted. ## Cocktail sort complexityNow, 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
**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(n**.^{2})**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(n**.^{2})
## 2. Space Complexity
- The space complexity of the cocktail sort is O(1).
## Implementation of cocktail sortNow, let's see the programs of cocktail sort in different programming languages.
So, that's all about the article. Hope the article will be helpful and informative to you. Next TopicCycle Sort |