Implement Binary Insertion Sort using Python

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 Python.


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, we’ll check if high <= low 


  • if the above condition is true then we’ll return (item > a[low]) ? (low + 1) : low  . Here item is the element at the preceding index in the array. 


  • Also, we’ll store the mid by performing mid = (low + high) / 2


  • Next, we’ll also check whether item == a[mid] and if it is then we’ll return mid+1


  • However if item > a[mid] is the case then we’ll return binarySearch(a, item,  mid + 1, high)


  • 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 Python


Python Program To Implement Binary Insertion Sort




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




Sorted array: 

0 12 17 23 31 37 46 54 72 88 100


# Python Program implementation
# of binary insertion sort

def binary_search(arr, val, start, end):	

    # to decide where to insert -1
    if start == end:
        if arr[start] > val:
            return start
            return start+1

    # position to find a number greater than val
    if start > end:
        return start

    mid = (start+end)//2
    if arr[mid] < val:
        return binary_search(arr, val, mid+1, end)
    elif arr[mid] > val:
        return binary_search(arr, val, start, mid-1)
        return mid

def insertion_sort(arr):
    for i in range(1, len(arr)):
        val = arr[i]
        j = binary_search(arr, val, 0, i-1)
        arr = arr[:j] + [val] + arr[j:i] + arr[i+1:]
    return arr

print("Sorted array:")
print(insertion_sort([37, 23, 0, 31, 22, 17, 12, 72, 31, 46, 100, 88, 54]))



I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *