Java Program for Binary Insertion Sort


Binary insertion sort uses the binary search to find the right position to insert an element at a specific index at every iteration. First, the location where the element needs to be inserted is found. Then, the elements are shifted to the next right location. Now, the specific element is placed in the position.

Following is the Java code for Binary Insertion Sort −

Example

 Live Demo

public class Demo{
   void Cocktail_Sort(int my_arr[]){
      boolean swapped = true;
      int start = 0;
      int end = my_arr.length;
      while (swapped == true) {
         swapped = false;
         for (int i = start; i < end - 1; ++i) {
            if (my_arr[i] > my_arr[i + 1]) {
               int temp = my_arr[i];
               my_arr[i] = my_arr[i + 1];
               my_arr[i + 1] = temp;
               swapped = true;
            }
         }
         if (swapped == false)
            break;
         swapped = false;
         end = end - 1;
         for (int i = end - 1; i >= start; i--) {
            if (my_arr[i] > my_arr[i + 1]) {
               int temp = my_arr[i];
               my_arr[i] = my_arr[i + 1];
               my_arr[i + 1] = temp;
               swapped = true;
            }
         }
         start = start + 1;
      }
   }
   void print_values(int my_arr[]){
      for (int i = 0; i < my_arr.length; i++)
      System.out.print(my_arr[i] + " ");
      System.out.println();
   }  
   public static void main(String[] args){
      Demo my_object = new Demo();
      int my_arr[] = { 6, 8, 34, 21, 0, 1, 98, 64, 6};
      System.out.println("The array contains ");
      for (int i = 0; i < my_arr.length; i++)
      System.out.print(my_arr[i] + " ");
      System.out.println();
      my_object.Cocktail_Sort(my_arr);
      System.out.println("The array after implementing cocktail sort is : ");
      my_object.print_values(my_arr);
   }
}

Output

The array contains
6 8 34 21 0 1 98 64 6
The array after implementing cocktail sort is :
0 1 6 6 8 21 34 64 98

Updated on: 04-Jul-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements