Binary Search

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.

diagram for binary insertion sort

Implementation of Binary Insertion Sort in C

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20implementation%20of%20binary%20insertion%20sort%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20binary%20search%20based%20function%20to%20find%20the%20position%0A%2F%2F%20where%20item%20should%20be%20inserted%20in%20a%5Blow..high%5D%0Aint%20binarySearch(int%20a%5B%5D%2C%20int%20item%2C%20int%20low%2C%20int%20high)%0A%7B%0A%20%20%20%20if%20(high%20%3C%3D%20low)%0A%20%20%20%20%20%20%20%20return%20(item%20%3E%20a%5Blow%5D)%3F%20%20(low%20%2B%201)%3A%20low%3B%0A%20%0A%20%20%20%20int%20mid%20%3D%20(low%20%2B%20high)%2F2%3B%0A%20%0A%20%20%20%20if(item%20%3D%3D%20a%5Bmid%5D)%0A%20%20%20%20%20%20%20%20return%20mid%2B1%3B%0A%20%0A%20%20%20%20if(item%20%3E%20a%5Bmid%5D)%0A%20%20%20%20%20%20%20%20return%20binarySearch(a%2C%20item%2C%20mid%2B1%2C%20high)%3B%0A%20%20%20%20return%20binarySearch(a%2C%20item%2C%20low%2C%20mid-1)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20sort%20an%20array%20a%5B%5D%20of%20size%20’n’%0Avoid%20insertionSort(int%20a%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20i%2C%20loc%2C%20j%2C%20k%2C%20selected%3B%0A%20%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%20n%3B%20%2B%2Bi)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20j%20%3D%20i%20-%201%3B%0A%20%20%20%20%20%20%20%20selected%20%3D%20a%5Bi%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20find%20location%20where%20selected%20sould%20be%20inseretd%0A%20%20%20%20%20%20%20%20loc%20%3D%20binarySearch(a%2C%20selected%2C%200%2C%20j)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Move%20all%20elements%20after%20location%20to%20create%20space%0A%20%20%20%20%20%20%20%20while%20(j%20%3E%3D%20loc)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20a%5Bj%2B1%5D%20%3D%20a%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20j–%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20a%5Bj%2B1%5D%20%3D%20selected%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20a%5B%5D%20%3D%20%7B37%2C%2023%2C%200%2C%2017%2C%2012%2C%2072%2C%2031%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2046%2C%20100%2C%2088%2C%2054%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(a)%2Fsizeof(a%5B0%5D)%2C%20i%3B%0A%20%0A%20%20%20%20insertionSort(a%2C%20n)%3B%0A%20%0A%20%20%20%20printf(%22Sorted%20array%3A%20%5Cn%22)%3B%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2Ca%5Bi%5D)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

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

Time Complexity: 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.

[ad type=”banner”]

Implementation of Binary Insertion Sort in JAVA

In this implementation, I have used library functions for binary search and shifting array to one location right

[pastacode lang=”java” manual=”package%20com.codingeek.algorithms.sorting%3B%0A%20%0Aimport%20java.util.Arrays%3B%0A%20%0Aclass%20BinaryInsertionSort%7B%0A%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20final%20int%5B%5D%20input%20%3D%20%7B%204%2C%2010%2C%203%2C%201%2C%209%2C%2015%20%7D%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Before%20Sorting%20-%20%22%20%2B%20Arrays.toString(input))%3B%0A%20%20%20%20%20%20%20%20new%20BinaryInsertionSort().sort(input)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22After%20Sorting%20-%20%22%20%2B%20Arrays.toString(input))%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%0A%20%20%20%20public%20void%20sort(int%20array%5B%5D)%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20array.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20x%20%3D%20array%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20location%20to%20insert%20using%20binary%20search%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20j%20%3D%20Math.abs(Arrays.binarySearch(array%2C%200%2C%20i%2C%20x)%20%2B%201)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FShifting%20array%20to%20one%20location%20right%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.arraycopy(array%2C%20j%2C%20array%2C%20j%2B1%2C%20i-j)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FPlacing%20element%20at%20its%20correct%20location%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20array%5Bj%5D%20%3D%20x%3B%0A%20%20%20%20%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]
Output:-
Before Sorting - [4, 10, 3, 1, 9, 15]
After Sorting - [1, 3, 4, 9, 10, 15]