Algorithm
Insertion Sort
Binary Search
Sorting Techniques
Computer Science

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.

Practice algorithms

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:

  1. Initialization: Assume the first element is sorted.
  2. Iteration: For each subsequent element, compare it with elements in the sorted section.
  3. Shifting Elements: Shift all larger elements one position to the right to make room for the unfixed element.
  4. 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 with 5 and insert before it. The array becomes [2, 5, 9, 1, 5, 6] .
  • Next, 9 is already greater than 5 , so it remains as is: [2, 5, 9, 1, 5, 6] .
  • With 1 , compare as follows: 1 < 9 , 1 < 5 , and 1 < 2 . Insert 1 at the start: [1, 2, 5, 9, 5, 6] .
  • The next element 5 needs to be inserted between 2 and 9 : [1, 2, 5, 5, 9, 6] .
  • Finally, 6 is placed after 5 : [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

  1. 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.
  2. Reduce Comparisons: This reduces comparisons to O(logn)O(\log n) for each insertion, compared to O(n)O(n) in the classic approach.
  3. 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 as 0 .
  • For 9 , its position remains 2 after searching in [2, 5] .
  • For 1 , its position is 0 after searching in [2, 5, 9] .
  • For the second 5 , determine its position as 3 .
  • For 6 , conduct a binary search in [1, 2, 5, 5, 9] , placing it at 5 .

This reduces the time complexity related to comparisons, although the act of inserting elements still requires O(n)O(n) 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, O(n2)O(n^2) due to shifts and comparisons. With binary search for element comparison, it improves to O(nlogn)O(n \log n) for comparisons; however, shifts still prevail at O(n2)O(n^2).
  • Space Complexity: O(1)O(1) as it is in-place.

Summary Table

FeatureClassic Insertion SortInsertion Sort with Binary Search
Comparison MechanismLinear SearchBinary Search
Position Finding TimeO(n)O(n)O(logn)O(\log n)
Total Time ComplexityO(n2)O(n^2)O(n2)O(n^2) (due to shifts)
Space ComplexityO(1)O(1)O(1)O(1)
Suitable forSmall or nearly sorted arraysLarger 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.