Cocktail Sort Program with Explanation and Code
2025-11-19
cocktail sort
shaker sort
bidirectional bubble sort
sorting algorithms
python cocktail sort
c cocktail sort
javascript cocktail sort
php cocktail sort
csharp cocktail sort
optimized bubble sort
Cocktail Sort is a bidirectional version of Bubble Sort that improves performance by sorting in both forward and backward passes. This page provides a clear explanation and complete code examples in Python, C, JavaScript, PHP, and C#.
Program Explanation
What is Cocktail Sort?
Cocktail Sort (also called Bidirectional Bubble Sort or Shaker Sort) is an improvement over Bubble Sort. Instead of moving through the list in only one direction, Cocktail Sort traverses the array in both forward and backward passes. This helps to push both the largest and smallest elements to their correct positions in each iteration.
Steps to Solve
- Start with a forward pass from the beginning of the array.
- Compare adjacent elements and swap if they are in the wrong order.
- After the forward pass, the largest element is moved to the end.
- Perform a backward pass from the end to the beginning.
- Swap smaller elements toward the front of the array.
- Repeat both passes while any swaps occur.
- The array becomes sorted when a full round completes with no swaps.
Key Points
- Improved version of Bubble Sort with bidirectional movement.
- Efficient in cases where small elements are near the end (turtles).
- Time complexity: O(n²) average and worst-case.
- Works in-place and uses constant extra space.
- Simple logic and easy to implement.
Program Code
def cocktail_sort(arr):
n = len(arr)
swapped = True
start = 0
end = n - 1
while swapped:
swapped = False
for i in range(start, end):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
if not swapped:
break
swapped = False
end -= 1
for i in range(end - 1, start - 1, -1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
start += 1
arr = list(map(int, input("Enter numbers: ").split()))
cocktail_sort(arr)
print("Sorted array:", arr)
#include <stdio.h>
void cocktailSort(int arr[], int n) {
int swapped = 1;
int start = 0, end = n - 1;
while (swapped) {
swapped = 0;
for (int i = start; i < end; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = 1;
}
}
if (!swapped)
break;
swapped = 0;
end--;
for (int i = end - 1; i >= start; i--) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = 1;
}
}
start++;
}
}
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
cocktailSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
function cocktailSort(arr) {
let start = 0;
let end = arr.length - 1;
let swapped = true;
while (swapped) {
swapped = false;
for (let i = start; i < end; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
swapped = true;
}
}
if (!swapped) break;
swapped = false;
end--;
for (let i = end - 1; i >= start; i--) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
swapped = true;
}
}
start++;
}
}
let arr = prompt("Enter numbers:").split(" ").map(Number);
cocktailSort(arr);
console.log("Sorted array:", arr);
using System;
class Program {
static void CocktailSort(int[] arr) {
int start = 0;
int end = arr.Length - 1;
bool swapped = true;
while (swapped) {
swapped = false;
for (int i = start; i < end; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
if (!swapped)
break;
swapped = false;
end--;
for (int i = end - 1; i >= start; i--) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
start++;
}
}
static void Main() {
Console.Write("Enter numbers: ");
int[] arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
CocktailSort(arr);
Console.Write("Sorted array: ");
foreach (int x in arr)
Console.Write(x + " ");
}
}
<?php
function cocktailSort(&$arr) {
$n = count($arr);
$start = 0;
$end = $n - 1;
$swapped = true;
while ($swapped) {
$swapped = false;
for ($i = $start; $i < $end; $i++) {
if ($arr[$i] > $arr[$i + 1]) {
$temp = $arr[$i];
$arr[$i] = $arr[$i + 1];
$arr[$i + 1] = $temp;
$swapped = true;
}
}
if (!$swapped)
break;
$swapped = false;
$end--;
for ($i = $end - 1; $i >= $start; $i--) {
if ($arr[$i] > $arr[$i + 1]) {
$temp = $arr[$i];
$arr[$i] = $arr[$i + 1];
$arr[$i + 1] = $temp;
$swapped = true;
}
}
$start++;
}
}
$arr = array_map('intval', explode(" ", readline("Enter numbers: ")));
cocktailSort($arr);
echo "Sorted array: " . implode(" ", $arr);
?>
