Algorithms | Sorting | Question 12

What is the worst case time complexity of insertion sort where position of the data to be inserted is calculated using binary search?

(A) N
(B) NlogN
(C) N^2
(D) N(logN)^2


Answer: (C)

Explanation: Applying binary search to calculate the position of the data to be inserted doesn’t reduce the time complexity of insertion sort. This is because insertion of a data at an appropriate position involves two steps:
1. Calculate the position.
2. Shift the data from the position calculated in step #1 one step right to create a gap where the data will be inserted.

Using binary search reduces the time complexity in step #1 from O(N) to O(logN). But, the time complexity in step #2 still remains O(N). So, overall complexity remains O(N^2).

Quiz of this Question



My Personal Notes arrow_drop_up