Implementation of Cocktail Sort in Java

Cocktail shaker sort is also knwn as double sided bubble sort. Some even say it’s also known as advance bubble sort or¬† variation of bubble sort. Therefore in this tutorial we will follow simple bubble sort technique to solve this question. The variation of this algorithm is due to the fact it sorts in both the direction on every pass through the traversal.

Example

Given Array:
[90, 80, 45, 2, 1, 10, 65]
The Sorted Array
[1, 2, 10, 45, 65, 80, 90]

By looking at the one can not differentiate any of the sorting algorithm. Now let’s see if the code helps.

Code

Lets start by making the cocktail class covering the algorithm discussed above.

import java.util.Arrays;
public class cocktail_Sort
{
    public void cocktail_Sort(int arr[])
    {
    boolean is_swapped;
    do {
    is_swapped = false;
    for (int i =0; i<=  arr.length  - 2;i++) {
      if (arr[ i ] > arr[ i + 1 ]) {
        
        int temp = arr[i];
        arr[i] = arr[i+1];
        arr[i+1]=temp;
        is_swapped = true;
      }
    }
    if (!is_swapped) {
      break;
    }
    is_swapped = false; // for the second time from back direction 
    for (int i= arr.length - 2;i>=0;i--) {
      if (arr[ i ] > arr[ i + 1 ]) {
        int temp = arr[i];
        arr[i] = arr[i+1];
        arr[i+1]=temp;
        is_swapped = true;
      }
    }
  } while (is_swapped);
}

Main Function

 public static void main(String args[])
    {
        cocktail_Sort s = new cocktail_Sort();
        int arr[] = {90, 80, 45, 2, 1, 10, 65};
        System.out.println("Given Array:");
        System.out.println(Arrays.toString(arr));
        s.cocktail_Sort(arr);
        System.out.println("The Sorted Array");
        System.out.println(Arrays.toString(arr));
    }
}

Full Code

import java.util.Arrays;
public class cocktail_Sort
{
    void cocktail_Sort(int arr[])
    {
    boolean is_swapped;
    do {
    is_swapped = false;
    for (int i =0; i<=  arr.length  - 2;i++) {
      if (arr[ i ] > arr[ i + 1 ]) {
        
        int temp = arr[i];
        arr[i] = arr[i+1];
        arr[i+1]=temp;
        is_swapped = true;
      }
    }
    if (!is_swapped) {
      break;
    }
    is_swapped = false;
    for (int i= arr.length - 2;i>=0;i--) {
      if (arr[ i ] > arr[ i + 1 ]) {
        int temp = arr[i];
        arr[i] = arr[i+1];
        arr[i+1]=temp;
        is_swapped = true;
      }
    }
  } while (is_swapped);
}
    
     public static void main(String args[])
    {
        cocktail_Sort s = new cocktail_Sort();
        int arr[] = {90, 80, 45, 2, 1, 10, 65};
        System.out.println("Given Array:");
        System.out.println(Arrays.toString(arr));
        s.cocktail_Sort(arr);
        System.out.println("The Sorted Array");
        System.out.println(Arrays.toString(arr));
    }
}

There is not many places one can do mistake but while going through the code make sure that you dry run the code for your better understanding.

Hope this was helpfull . Happy Coding !.

Leave a Reply