TUTORIAL · Algorithm
Binary Sort
Learn how binary insertion sort finds insertion positions with binary search, and why shifting still takes quadratic time.
On this page
Binary insertion sort is insertion sort with a binary search for each new element’s position in the sorted prefix. The search reduces comparisons, but elements still have to move to make room. The method is useful for understanding the trade-off between comparison cost and movement cost; it does not make array-based insertion sort an O(n log n) algorithm. See the insertion sort algorithm for the linear-search version.
What binary insertion sort does
The algorithm divides an array into a sorted prefix and an unsorted suffix. Initially, the first element is the sorted prefix. On each pass, it saves the next suffix element as the key, searches the prefix to find where that key belongs, shifts the larger elements one position to the right, and places the key in the opened slot. After the final pass, the whole array is sorted.
Binary search finds a position using logarithmically many comparisons in the prefix. It does not create a free slot: in a contiguous array, inserting near the front can still move almost every element in that prefix.
How binary insertion sort works
For input (5, 3, 4, 2, 1, 6), the sorted prefix grows by one element per pass. The insertion position below is an index in the prefix before shifting; the array column shows the full state after insertion.
| Key | Insertion index | Array after insertion |
|---|---|---|
| — | — | (5, 3, 4, 2, 1, 6) |
3 |
0 |
(3, 5, 4, 2, 1, 6) |
4 |
1 |
(3, 4, 5, 2, 1, 6) |
2 |
0 |
(2, 3, 4, 5, 1, 6) |
1 |
0 |
(1, 2, 3, 4, 5, 6) |
6 |
5 |
(1, 2, 3, 4, 5, 6) |
There are five passes because the first element is already the initial sorted prefix. The last key, 6, belongs at the end, so no elements need to shift on that pass.
C++ implementation
This implementation uses an iterative upper-bound search: when a value equals the key, the search continues to the right. Inserting after existing equal values preserves their original relative order. The example is self-contained and produces the following output:
#include <iostream>
#include <vector>
std::size_t insertion_position(const std::vector<int>& values,
std::size_t end, int key) {
std::size_t low = 0;
std::size_t high = end;
while (low < high) {
const std::size_t middle = low + (high - low) / 2;
if (key < values[middle]) {
high = middle;
} else {
low = middle + 1;
}
}
return low;
}
void binary_insertion_sort(std::vector<int>& values) {
for (std::size_t i = 1; i < values.size(); ++i) {
const int key = values[i];
const std::size_t position = insertion_position(values, i, key);
for (std::size_t j = i; j > position; --j) {
values[j] = values[j - 1];
}
values[position] = key;
}
}
int main() {
std::vector<int> values{5, 3, 4, 2, 1, 6};
binary_insertion_sort(values);
std::cout << "Sorted: ";
for (std::size_t i = 0; i < values.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << values[i];
}
std::cout << '\n';
}
Sorted: 1 2 3 4 5 6
The search range is [0, i), so it never includes the key’s current slot. The shift loop copies values from right to left, preventing an element from being overwritten before it is moved. If the vector has fewer than two elements, the loop performs no passes and leaves it unchanged.
Complexity and trade-offs
Each of the n - 1 passes performs a binary search with O(log n) comparisons, for O(n log n) comparisons overall. The movement cost is different: in the average and worst cases, a pass can shift a number of elements proportional to the prefix length. Across all passes, that cost is O(n²), so average and worst-case running time remain O(n²). For an already sorted array, each of the n - 1 passes still performs a binary search over a growing prefix, so the searches take O(n log n) time overall even though no elements shift.
The implementation sorts the vector in place and uses O(1) auxiliary space. Its stable behavior depends on choosing the position after equal values, as this implementation does. Choosing the position before equal values can reorder equal elements and make the sort unstable.
When to use binary insertion sort
Use it when the input is small or nearly sorted and reducing comparisons is useful, especially when comparisons are relatively expensive. It may also be useful as a component in a larger sorting strategy. For large arrays, the quadratic movement cost is usually a poor fit; use a sorting algorithm with O(n log n) time instead. Binary insertion sort improves where the search happens, not how many elements an array may need to move.