Binary Sort (Binary Insertion Sort)
Binary Sort is also known as Binary Insertion Sort. It is a variation of the Insertion Sort algorithm.
The only difference is that instead of scanning the sorted portion linearly to find the correct position for insertion, it uses Binary Search to find the position, making the search faster.
In Binary Insertion Sort, the number of comparisons is reduced by using binary search, but the number of shifts remains the same as normal Insertion Sort.
How Does It Work?
- Start from the second element in the array because the first element is already considered "sorted".
- Find the correct position in the sorted left part of the array.
- Binary search to quickly find where the key should be inserted
- Use
low
andhigh
pointers to define the current search range within the sorted part.- Calculate the middle index (
mid
) usingmid = (low + high) / 2
- Compare the key with the element at
mid
- If
key < arr[mid]
, it must go to the left, so movehigh = mid - 1
- If
key > arr[mid]
, it must go to the right, so movelow = mid + 1
- If
- Repeat this until
low > high
. At this point,low
is the correct insert position.
- Calculate the middle index (
- Shift all elements to the right to make space for the key.
- Insert the key at the position
low
. - Repeat steps for all remaining elements in the array.
Example: Binary (Insertion) Sort
Let’s solve the array {37, 23, 0, 17, 12, 72, 31}
using Binary Insertion Sort, pass by pass, in clear and detailed explanation.
Initial Array is {37, 23, 0, 17, 12, 72, 31}
We’ll insert each element (from index 1 to 6) into the correct position in the sorted part using binary search, and then shift elements to insert.
Pass 1 (i = 1, key = 23)
Sorted part: [37]
Use binary search to find where to insert 23 in [37].
- low = 0, high = 0
- mid = (0 + 0) / 2 = 0
- 23 < 37 → high = mid - 1 = -1
- Now low = 0, high = -1 → stop
- Insert at position low = 0
- Shift 37 right
Array now: [23, 37, 0, 17, 12, 72, 31]
Pass 2 (i = 2, key = 0)
Sorted part: [23, 37]
Binary search for 0:
- low = 0, high = 1
- mid = (0 + 1) / 2 = 0
- 0 < 23 → high = mid - 1 = -1
- low = 0, high = -1 → stop
- Insert at position low = 0
- Shift 23 and 37 right
Array now: [0, 23, 37, 17, 12, 72, 31]
Pass 3 (i = 3, key = 17)
Sorted part: [0, 23, 37]
Binary search for 17:
- low = 0, high = 2
- mid = (0 + 2) / 2 = 1
- 17 < 23 → high = mid - 1 = 0
- mid = (0 + 0) / 2 = 0
- 17 > 0 → low = mid + 1 = 1
- low = 1, high = 0 → stop
- Insert at position low = 1
- Shift 23 and 37 right
Array now: [0, 17, 23, 37, 12, 72, 31]
Pass 4 (i = 4, key = 12)
Sorted part: [0, 17, 23, 37]
Binary search for 12:
- low = 0, high = 3
- mid = (0 + 3) / 2 = 1
- 12 < 17 → high = mid - 1 = 0
- mid = (0 + 0) / 2 = 0
- 12 > 0 → low = mid + 1 = 1
- low = 1, high = 0 → stop
- Insert at position low = 1
- Shift 17, 23, 37 right
Array now: [0, 12, 17, 23, 37, 72, 31]
Pass 5 (i = 5, key = 72)
Sorted part: [0, 12, 17, 23, 37]
Binary search for 72:
- low = 0, high = 4
- mid = (0 + 4) / 2 = 2
- 72 > 17 → low = mid + 1 = 3
- mid = (3 + 4) / 2 = 3
- 72 > 23 → low = mid + 1 = 4
- mid = (4 + 4) / 2 = 4
- 72 > 37 → low = mid + 1 = 5
- low = 5, high = 4 → stop
- Insert at position low = 5 (already there)
Array now: [0, 12, 17, 23, 37, 72, 31]
Pass 6 (i = 6, key = 31)
Sorted part: [0, 12, 17, 23, 37, 72]
Binary search for 31:
- low = 0, high = 5
- mid = (0 + 5) / 2 = 2
- 31 > 17 → low = mid + 1 = 3
- mid = (3 + 5) / 2 = 4
- 31 < 37 → high = mid - 1 = 3
- mid = (3 + 3) / 2 = 3
- 31 > 23 → low = mid + 1 = 4
- low = 4, high = 3 → stop
- Insert at position low = 4
- Shift 37 and 72 right
Final Sorted Array: {0, 12, 17, 23, 31, 37, 72}
Summary Table
Pass | Inserted | Binary Pos | Action | Resulting Array |
---|---|---|---|---|
1 | 23 | 0 | Shift 37 → Insert at 0 | {23, 37, 0, 17, 12, 72, 31} |
2 | 0 | 0 | Shift 23, 37 → Insert at 0 | {0, 23, 37, 17, 12, 72, 31} |
3 | 17 | 1 | Shift 23, 37 → Insert at 1 | {0, 17, 23, 37, 12, 72, 31} |
4 | 12 | 1 | Shift 17, 23, 37 → Insert at 1 | {0, 12, 17, 23, 37, 72, 31} |
5 | 72 | 5 | No shift → Insert at 5 | {0, 12, 17, 23, 37, 72, 31} |
6 | 31 | 4 | Shift 37, 72 → Insert at 4 | {0, 12, 17, 23, 31, 37, 72} |
Where to Use Binary Sort?
In educational tools to demonstrate optimisation techniques.
When binary search is already implemented.
For small or partially sorted data.
To combine search and sorting logic simply.
Time and Space Complexity
Case | Comparisons | Shifts | Time Complexity |
---|---|---|---|
Best Case | O(log n) per insert | O(1) | O(n log n) |
Average Case | O(log n) per insert | O(n) | O(n²) |
Worst Case | O(log n) per insert | O(n) | O(n²) |
Space | – | – | O(1) (in-place) |
Advantages of Binary Sort
Fewer comparisons than normal insertion sort.
In-place sorting (no extra space needed).
Efficient on small or nearly sorted arrays.
Good for learning binary search and insertion concepts together.
Disadvantages of Binary Sort
O(n²) in worst case due to shifting.
Not suitable for large datasets.
Binary search doesn't help reduce shifting operations.
Algorithm for Binary Sort
Step 1: Repeat for i = 1 to n - 1
- Take the value at index i → call it key
- Set low = 0, high = i - 1
Step 2: Use Binary Search to find position
- While low ≤ high
- Set mid = (low + high) / 2
- If key < arr[mid], set high = mid - 1
- Else, set low = mid + 1
- Now, low is the correct position to insert key
Step 3: Shift all elements from index i-1 to low
- For j = i - 1 down to low
- Move arr[j] to arr[j + 1]
Step 4: Place key at arr[low]
Step 5: Repeat until all elements are sorted
C Program for Binary Sort
#include <stdio.h>
// Binary Search to find position to insert
int binarySearch(int arr[], int item, int low, int high) {
while (low <= high) {
int mid = (low + high) / 2;
if (item == arr[mid])
return mid + 1;
else if (item > arr[mid])
low = mid + 1;
else
high = mid - 1;
}
return low;
}
// Binary Insertion Sort Function
void binaryInsertionSort(int arr[], int n) {
int i, j, pos, selected;
for (i = 1; i < n; ++i) {
selected = arr[i];
pos = binarySearch(arr, selected, 0, i - 1);
// Shift elements to make space
for (j = i - 1; j >= pos; --j)
arr[j + 1] = arr[j];
arr[pos] = selected;
}
}
// Display Array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {37, 23, 0, 17, 12, 72, 31};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original Array:\n");
printArray(arr, n);
binaryInsertionSort(arr, n);
printf("Sorted Array:\n");
printArray(arr, n);
return 0;
}
Output
Original array:
16 14 10 8 7 9 3 2 4 1
Sorted array using Heap Sort:
1 2 3 4 7 8 9 10 14 16
Why Is This Better Than Insertion Sort?
Normal Insertion Sort uses linear search to find position — O(n) time.
Binary Insertion Sort uses binary search — O(log n) time.
So it saves time when comparing large or complex data.
Numerical Questions for Binary Sort
- Sort the following array using Binary Insertion Sort
{29, 15, 10, 40, 7}
. Show the steps of binary search and insertion at each pass. - Sort this array using Binary Insertion Sort
{8, 4, 1, 56, 3, -44, 23, -6}
. Show how the position of each element is found using binary search, and how elements are shifted. - Apply Binary Insertion Sort on
{45, 12, 5, 78, 31, 6}
. Show the array after each pass. - Sort the
{22, 11, 99, 88, 9, 7, 42}
using Binary Insertion Sort and count- Number of comparisons
- Number of shifts
- Given this array
{34, 21, 13, 89, 55, 3}
. Manually trace the binary search range for each pass and the final sorted array.