Insertion Sort with binary search
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Insertion sort is a simple and intuitive sorting algorithm that has inspired many variations to improve its efficiency in specific scenarios. One such optimization involves using binary search to minimize the number of comparisons needed to insert each element in its correct place.
Understanding Insertion Sort
Insertion sort organizes a list of elements partly sorted from left to right. It starts with the second element and moves it leftward, past any larger elements, to its correct place in the sorted section of the list. This process repeats for each element until the list is sorted. Here’s the basic step-by-step process:
- Initialization: Assume the first element is sorted.
- Iteration: For each subsequent element, compare it with elements in the sorted section.
- Shifting Elements: Shift all larger elements one position to the right to make room for the unfixed element.
- Placement: Insert the unfixed element into the correct position.
Example
Consider sorting the following array using insertion sort:
[5, 2, 9, 1, 5, 6]
Step-by-step Process:
- Start with the first element
[5]. It is already sorted. - Move to the second element
2. Compare with5and insert before it. The array becomes[2, 5, 9, 1, 5, 6]. - Next,
9is already greater than5, so it remains as is:[2, 5, 9, 1, 5, 6]. - With
1, compare as follows:1 < 9,1 < 5, and1 < 2. Insert1at the start:[1, 2, 5, 9, 5, 6]. - The next element
5needs to be inserted between2and9:[1, 2, 5, 5, 9, 6]. - Finally,
6is placed after5:[1, 2, 5, 5, 6, 9].
Introducing Binary Search to Insertion Sort
The primary inefficiency in insertion sort arises from its linear scan to determine the correct position for each element. By utilizing a binary search, we can reduce these comparisons to logarithmic time, enhancing the overall efficiency for larger datasets.
How Binary Search Works with Insertion Sort
- Binary Search for Positioning: Instead of searching linearly, apply binary search on the sorted portion of the array to find the correct insertion position for the current element.
- Reduce Comparisons: This reduces comparisons to for each insertion, compared to in the classic approach.
- Shifting: Once the position is determined, elements are shifted as usual.
Detailed Example
Let's apply this approach to the array [5, 2, 9, 1, 5, 6]
.
- For
2, perform a binary search in[5]to find its placement position as0. - For
9, its position remains2after searching in[2, 5]. - For
1, its position is0after searching in[2, 5, 9]. - For the second
5, determine its position as3. - For
6, conduct a binary search in[1, 2, 5, 5, 9], placing it at5.
This reduces the time complexity related to comparisons, although the act of inserting elements still requires shifts in the worst case.
Complexity Analysis
- Time Complexity:
- Best Case (
O(n)): The array is already sorted. - Average/Worst Case: In the classical method, due to shifts and comparisons. With binary search for element comparison, it improves to for comparisons; however, shifts still prevail at .
- Space Complexity: as it is in-place.
Summary Table
| Feature | Classic Insertion Sort | Insertion Sort with Binary Search |
| Comparison Mechanism | Linear Search | Binary Search |
| Position Finding Time | ||
| Total Time Complexity | (due to shifts) | |
| Space Complexity | ||
| Suitable for | Small or nearly sorted arrays | Larger datasets where comparisons can benefit from binary search |
Conclusion
Insertion sort enriched with binary search offers an elegant enhancement to a fundamental algorithm. While it doesn’t entirely change the asymptotic complexity due to shifts, it nevertheless optimizes the number of comparisons significantly. This method, therefore, serves well in scenarios where element comparison is costly or when dealing with substantially larger datasets where even minor improvements contribute to overall efficiency.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.