If you're in a computer science background, widening your technical horizons is the best thing you should do. However, your programming skills matter the most and one must continuously keep sharpening these skills. And to become a better programmer in the future. Though there are multiple things to focus on, one specific area you must focus on is the world of data structures. Data structures in general are specific approaches to solve problems in such a way that computer resources get used minimum. In general, there are multiple data structures you can learn and implement as well. However, to keep things simple were going to start with some basic programs you must learn before moving on to complex algorithms. Therefore today we're going to learn how to Implement Binary Insertion Sort using Java.

 

What is Binary Insertion Sort?

 

  • Comes in the list of simplest data structures, Binary Insertion Sort is the advanced and efficient way of arranging elements in ascending order. The array is either sorted or is unsorted.

 

  • The key thing separating Binary Insertion Sort from Insertion Sort is that to search for the swapping element we apply a binary search algorithm. Applying Binary Search reduces the time complexity.

 

  • When the array is sorted this data structure works quickest. However, when elements aren't sorted it works the slowest.

 

What's The Approach?

 

  • Consider arr[low..high] of size n we want to implement Binary Insertion Sort on. So firstly we'll iterate a loop from 1 to n.

 

  • Next, we'll compare the element at iterating index with its preceding element from the array. If the comparing element is smaller, we'll again compare it with the before elements.

 

  •  We will use binary search to find the preceding element so, for that, we'll use the built java method of binary search, i.e  Arrays.binarySearch()

 

  • Lastly, we'll Move the greater elements one position up to make space for the swapped element.

 

Also Read: Print Prime Factors of A Number in Java

 

Java Program To Implement Binary Insertion Sort

 

Input:

 

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

 

Output:

 

Sorted array: 

0 12 17 23 31 37 46 54 72 88 100

 

// Java Program implementing  // binary insertion sort    import java.util.Arrays;  class TechDecodeTutorials  {        public static void main(String[] args)      {          final int[] arr = { 37, 23, 0, 17, 12, 72,                              31, 46, 100, 88, 54 };            new TechDecodeTutorials().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;          }      }  }