Binary Insertion Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Binary Insertion Sort
Insertion Sort with a smarter probe: use binary search to find where the new element belongs in O(log n), then shift.
How it works
Plain Insertion Sort does up to i comparisons to find the insertion point for element i. Binary Insertion Sort replaces that linear probe with a binary search over the sorted prefix.
- Pick the next element as the key.
- Binary-search the sorted prefix
arr[0..i-1]for the leftmost position where the key would still respect order. - Shift the suffix one slot right.
- Insert the key in the freed slot.
The total comparison count drops from Θ(n²) to Θ(n log n) — useful when comparisons are very expensive (think: comparing long strings). The shift step, however, is still Θ(n²) writes in the worst case.
Complexity
Arrays.sort for object arrays once used binary insertion sort for small runs — comparisons there involve calling compareTo, so cutting them was worth the extra shift work.Interactive visualization
Pseudocode
procedure binaryInsertionSort(A): for i from 1 to n − 1: key ← A[i] pos ← binarySearch(A, 0, i, key) shift A[pos..i − 1] right by 1 A[pos] ← key
JavaScript implementation
function binaryInsertionSort(arr) { for (let i = 1; i < arr.length; i++) { const key = arr[i]; let lo = 0, hi = i; while (lo < hi) { const mid = (lo + hi) >> 1; if (arr[mid] <= key) lo = mid + 1; else hi = mid; } for (let j = i; j > lo; j--) arr[j] = arr[j - 1]; arr[lo] = key; } return arr; }
Pros and cons
Pros
- Cuts comparisons to O(n log n) — big win when comparisons are costly.
- Stable when the search uses an upper-bound (lower-bound + key <=) probe.
- Still in-place; constant extra memory.
Cons
- Shift cost is still O(n²) writes worst case.
- Slightly more complex than plain insertion sort.
- Loses when comparison cost is cheap relative to memory writes.
When to use it
- Sorting object arrays with expensive comparators.
- Small runs in hybrid sorts when comparisons dominate.
- Stability + low comparison count is required and the array is small.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid