Java Program for Cocktail Sort



In this article, we will learn to perform cocktail sorting in Java. Cocktail Sort, also known as Bidirectional Bubble Sort, is a variation of the standard Bubble Sort algorithm.

What is Cocktail Sort?

Cocktail Sort works in contrast to bubble sort, wherein elements are iterated from left to right, and the largest element is first brought to its correct position, and so on. In cocktail sort, elements are iterated over in both directions (left and right) in an alternating fashion.

Cocktail Sort Working

Following are the steps to perform cocktail sort in Java ?

  • Traverse the array from left to right, pushing the largest element to the right (similar to Bubble Sort).
  • Traverse the array from right to left, pushing the smallest element to the left.
  • Repeat the process until the array is sorted.
  • The algorithm stops early if no swaps occur in a pass, indicating the list is already sorted.

Forward Pass: Bubble the largest element to the right ?

for (int i = start; i < end; i++) {
          if (arr[i] > arr[i + 1]) {
                swap(arr, i, i + 1);
                swapped = true;
           }
}

If no swaps, the array is sorted ?

 if (!swapped) break;

Backward Pass: Bubble the smallest element to the left ?

for (int i = end - 1; i >= start; i--) {
            if (arr[i] > arr[i + 1]) {
                swap(arr, i, i + 1);
                swapped = true;
            }
}

Example

Following is the program for Cocktail Sort in Java ?

import java.util.Arrays;

public class CocktailSort {
    public static void cocktailSort(int[] arr) {
        boolean swapped;
        int start = 0;
        int end = arr.length - 1;

        do {
            swapped = false;
            for (int i = start; i < end; i++) {
                if (arr[i] > arr[i + 1]) {
                    swap(arr, i, i + 1);
                    swapped = true;
                }
            }
            if (!swapped) break;

            swapped = false;
            for (int i = end - 1; i >= start; i--) {
                if (arr[i] > arr[i + 1]) {
                    swap(arr, i, i + 1);
                    swapped = true;
                }
            }
            start++;

        } while (swapped);
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 4, 2, 7, 1, 9};
        System.out.println("Original Array: " + Arrays.toString(arr));
        cocktailSort(arr);
        System.out.println("Sorted Array: " + Arrays.toString(arr));
    }
}

Output

The sorted array is
0 1 12 32 34 67 78 90 95

Time Complexity: O(n^2), similar to Bubble Sort.
Space Complexity: O(1), sorting is done in-place, using only a few extra variables.

Conclusion

Cocktail Sort is an easy optimization of Bubble Sort which performs quite well for small or almost sorted lists. Yet for large data, more effective algorithms such as Quick Sort or Merge Sort are utilized.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-03-10T19:41:25+05:30

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements