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 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 -
This process continues until the array elements are not sorted. Now, let's see the algorithm of cocktail sort algorithm. AlgorithmWorking 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 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. 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. 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. 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 - 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. 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 - Second Backward pass: Now, the second backward pass is started. So, from backward direction, array elements 3 and 2 will be compared. After swapping, the array will be - 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. 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
2. Space Complexity
Implementation of cocktail sortNow, let's see the programs of cocktail sort in different programming languages. Program: Write a program to implement cocktail sort in C language. Output: Program: Write a program to implement cocktail sort in C++. Output: Program: Write a program to implement cocktail sort in C#. Output: Program: Write a program to implement cocktail sort in Java. Output: So, that's all about the article. Hope the article will be helpful and informative to you. Next TopicCycle Sort |