Binary Insertion Sort Python (original) (raw)

Last Updated : 7 Nov, 2025

Given an array of integers, the task is to sort the elements in ascending order using Binary Insertion Sort. Unlike the traditional insertion sort, this algorithm uses binary search to find the correct position for inserting an element, which reduces unnecessary comparisons and makes sorting more efficient while maintaining a stable order.

**For Example:

**Input: [37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54]
**Output: [0, 12, 17, 23, 31, 37, 46, 54, 72, 88, 100]
**Explanation: The algorithm places each unsorted element into its correct position in the sorted portion of the array using binary search, resulting in a fully sorted list.

Algorithm Steps

  1. Start with the second element (index 1) since the first element is considered sorted.
  2. Perform binary search on the sorted part of the array (from index 0 to i-1) to find the correct position for the current element.
  3. Shift all elements greater than the current element one position ahead to make space.
  4. Insert the current element at its correct position found by binary search.
  5. Repeat steps 2-4 for all remaining elements in the array until it becomes fully sorted.

Using User-Defined Binary Search Function

In this approach, we manually implement the binary search logic to find the correct position for inserting elements during sorting. This helps understand how binary insertion sort works internally.

Python `

def binary_search(arr, val, start, end): if start == end: if arr[start] > val: return start else: return start + 1 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)
else:
    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, 17, 12, 72, 31, 46, 100, 88, 54]))

`

Output

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

**Explanation:

Using bisect Module

In this method, the built-in bisect module is used to find the position where an element should be inserted to maintain the sorted order. The function bisect_left() returns the index where the element should go, reducing the need for manual binary search logic.

Python `

import bisect

def binary_search(arr, val, start, end): # Find position using bisect_left within arr[start:end+1] idx = bisect.bisect_left(arr[start:end+1], val) return start + idx

def insertion_sort(arr): for i in range(1, len(arr)): val = arr[i] j = binary_search(arr, val, 0, i-1) # Insert the element at the correct position arr = arr[:j] + [val] + arr[j:i] + arr[i+1:] return arr

print("Sorted array:") print(insertion_sort([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]

**Explanation:

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