Open In App

Java Program for Binary Insertion Sort

We can use binary search to reduce the number of comparisons in normal insertion sort. Binary Insertion Sort find use binary search to find the proper location to insert the selected item at each iteration.  In normal insertion, sort it takes O(i) (at ith iteration) in worst case. we can reduce it to O(logi) by using binary search.

How Does Binary insertion sort Work?

Approach to implement Binary Insertion sort:

  • Iterate the array from the second element to the last element.
  • Store the current element A[i] in a variable key.
  • Find the position of the element just greater than A[i] in the subarray from A[0] to A[i-1] using binary search. Say this element is at index pos.
  • Shift all the elements from index pos to i-1 towards the right.
  • A[pos] = key.

Below is the implementation for the above approach:






// Java Program implementing
// binary insertion sort
 
import java.util.Arrays;
class GFG {
 
    public static void main(String[] args)
    {
        final int[] arr = { 37, 23, 0,   17, 12, 72,
                            31, 46, 100, 88, 54 };
 
        new GFG().sort(arr);
 
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver Code
    public void sort(int array[])
    {
        for (int i = 1; i < array.length; i++) {
            int x = array[i];
 
            // Find location to insert
            // using binary search
            int j = Math.abs(
                Arrays.binarySearch(array, 0, i, x) + 1);
 
            // Shifting array to one
            // location right
            System.arraycopy(array, j, array, j + 1, i - j);
 
            // Placing element at its
            // correct location
            array[j] = x;
        }
    }
}
 
// Code contributed by Mohit Gupta_OMG

Output
0 12 17 23 31 37 46 54 72 88 100 

Time Complexity: O(n2), the algorithm as a whole still has a running worst-case running time of O(n2) because of the series of swaps required for each insertion. 
Auxiliary Space: O(1)



Iterative Approach:

Below is the implementation for the above approach:




import java.io.*;
 
class GFG {
 
    // iterative implementation
    static int binarySearch(int a[], int item, int low,
                            int high)
    {
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (item == a[mid])
                return mid + 1;
            else if (item > a[mid])
                low = mid + 1;
            else
                high = mid - 1;
        }
 
        return low;
    }
 
    // Function to sort an array a[] of size 'n'
    static void insertionSort(int a[], int n)
    {
        int i, loc, j, k, selected;
 
        for (i = 1; i < n; ++i) {
            j = i - 1;
            selected = a[i];
 
            // find location where selected should be
            // inserted
            loc = binarySearch(a, selected, 0, j);
 
            // Move all elements after location to create
            // space
            while (j >= loc) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = selected;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int a[] = { 37, 23, 0,   17, 12, 72,
                    31, 46, 100, 88, 54 };
        int n = a.length, i;
 
        insertionSort(a, n);
 
        System.out.println("Sorted array:");
        for (i = 0; i < n; i++)
            System.out.print(a[i] + " ");
    }
}
 
// This code is contributed by shivanisinghss2110.

Output
Sorted array:
0 12 17 23 31 37 46 54 72 88 100 

Time Complexity: O(n*log n)
Auxiliary space: O(1)

Applications of Binary Insertion sort:

Please refer complete article on Binary Insertion Sort for more details!


Article Tags :