|
| 1 | +-- |
| 2 | +id: Cocktail-Sort |
| 3 | +title: Cocktail Sort (Geeks for Geeks) |
| 4 | +sidebar_label: Cocktail Sort |
| 5 | +tags: |
| 6 | + - Intermediate |
| 7 | + - Sorting Algorithms |
| 8 | + - Geeks for Geeks |
| 9 | + - CPP |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - JavaScript |
| 13 | + - DSA |
| 14 | +description: "This is a solution to the Cocktail Sort problem on Geeks for Geeks." |
| 15 | +--- |
| 16 | + |
| 17 | +## 1. What is Cocktail Sort? |
| 18 | + |
| 19 | +Cocktail Sort is a variation of Bubble Sort. It traverses the list in both directions alternatively. This algorithm is also known as Bidirectional Bubble Sort or Shaker Sort. |
| 20 | + |
| 21 | +## 2. Algorithm for Cocktail Sort |
| 22 | + |
| 23 | +1. Start at the beginning of the list. |
| 24 | +2. Traverse the list from left to right, swapping adjacent items if they are in the wrong order. |
| 25 | +3. When the end of the list is reached, reverse the direction and traverse from right to left, again swapping adjacent items if they are in the wrong order. |
| 26 | +4. Repeat steps 2 and 3 until the list is sorted. |
| 27 | + |
| 28 | +## 3. How does Cocktail Sort work? |
| 29 | + |
| 30 | +- It sorts in both directions in each pass through the list, which can help elements move into place faster. |
| 31 | +- This bidirectional approach allows earlier elements to "bubble up" and later elements to "bubble down" in the same pass, potentially reducing the number of overall passes needed. |
| 32 | + |
| 33 | +## 4. Problem Description |
| 34 | + |
| 35 | +Given an array of integers, implement the Cocktail Sort algorithm to sort the array. |
| 36 | + |
| 37 | +## 5. Examples |
| 38 | + |
| 39 | +**Example 1:** |
| 40 | + |
| 41 | +``` |
| 42 | +Input: [5, 1, 4, 2, 8, 0, 2] |
| 43 | +Output: [0, 1, 2, 2, 4, 5, 8] |
| 44 | +``` |
| 45 | +**Example 2:** |
| 46 | +``` |
| 47 | +Input: [5, 1, 4, 2, 9, 8] |
| 48 | +Output: [1, 2, 4, 5, 8, 9] |
| 49 | +``` |
| 50 | + |
| 51 | +## 6. Constraints |
| 52 | + |
| 53 | +- The array should contain at least one element. |
| 54 | + |
| 55 | +## 7. Implementation |
| 56 | + |
| 57 | +**Python** |
| 58 | +```python |
| 59 | +def cocktail_sort(arr): |
| 60 | + n = len(arr) |
| 61 | + swapped = True |
| 62 | + start = 0 |
| 63 | + end = n - 1 |
| 64 | + while swapped: |
| 65 | + swapped = False |
| 66 | + for i in range(start, end): |
| 67 | + if arr[i] > arr[i + 1]: |
| 68 | + arr[i], arr[i + 1] = arr[i + 1], arr[i] |
| 69 | + swapped = True |
| 70 | + if not swapped: |
| 71 | + break |
| 72 | + swapped = False |
| 73 | + end -= 1 |
| 74 | + for i in range(end - 1, start - 1, -1): |
| 75 | + if arr[i] > arr[i + 1]: |
| 76 | + arr[i], arr[i + 1] = arr[i + 1], arr[i] |
| 77 | + swapped = True |
| 78 | + start += 1 |
| 79 | + return arr |
| 80 | +``` |
| 81 | +```java |
| 82 | +import java.util.Arrays; |
| 83 | + |
| 84 | +public class CocktailSort { |
| 85 | + public static void cocktailSort(int[] arr) { |
| 86 | + boolean swapped = true; |
| 87 | + int start = 0; |
| 88 | + int end = arr.length - 1; |
| 89 | + |
| 90 | + while (swapped) { |
| 91 | + swapped = false; |
| 92 | + for (int i = start; i < end; i++) { |
| 93 | + if (arr[i] > arr[i + 1]) { |
| 94 | + int temp = arr[i]; |
| 95 | + arr[i] = arr[i + 1]; |
| 96 | + arr[i + 1] = temp; |
| 97 | + swapped = true; |
| 98 | + } |
| 99 | + } |
| 100 | + if (!swapped) break; |
| 101 | + swapped = false; |
| 102 | + end--; |
| 103 | + for (int i = end - 1; i >= start; i--) { |
| 104 | + if (arr[i] > arr[i + 1]) { |
| 105 | + int temp = arr[i]; |
| 106 | + arr[i] = arr[i + 1]; |
| 107 | + arr[i + 1] = temp; |
| 108 | + swapped = true; |
| 109 | + } |
| 110 | + } |
| 111 | + start++; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public static void main(String[] args) { |
| 116 | + int[] arr = {5, 1, 4, 2, 8, 0, 2}; |
| 117 | + cocktailSort(arr); |
| 118 | + System.out.println(Arrays.toString(arr)); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +``` |
| 123 | + |
| 124 | +## 8. Complexity Analysis |
| 125 | + |
| 126 | +- **Time Complexity**: |
| 127 | + - Best case: $O(n)$ (when the array is already sorted) |
| 128 | + Average case: $O(n^2)$ |
| 129 | + Worst case: $O(n^2)$ |
| 130 | + |
| 131 | +- **Space Complexity**: $O(1)$ (in-place sorting) |
| 132 | + |
| 133 | +## 9. Advantages and Disadvantages |
| 134 | + |
| 135 | +**Advantages:** |
| 136 | +- Cocktail Sort can be more efficient than Bubble Sort for certain types of input because it can address issues where small elements are initially near the end of the list. |
| 137 | +- Simple to understand and implement. |
| 138 | + |
| 139 | +**Disadvantages:** |
| 140 | +- Still has a worst-case time complexity of $O(n^2)$, making it inefficient on large lists compared to more advanced algorithms like Quick Sort, Merge Sort, or Heap Sort. |
| 141 | +- The bidirectional approach does not significantly improve performance for most input cases. |
| 142 | + |
| 143 | +## 10. References |
| 144 | + |
| 145 | +- **GFG Article on Counting Sort:** [Geeks for Geeks Counting Sort](https://www.geeksforgeeks.org/cocktail_sort/) |
| 146 | +- **GFG Problem** [Counting Sort Problem](https://www.geeksforgeeks.org/problems/cocktail-sort/1) |
| 147 | +- **Wikipedia Article on Counting Sort:** [Counting Sort - Wikipedia](https://en.wikipedia.org/wiki/cocktail_sort) |
0 commit comments