What is the worst case time complexity of insertion sort where positio...
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).
View all questions of this testWhat is the worst case time complexity of insertion sort where positio...
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).
What is the worst case time complexity of insertion sort where positio...
Explanation:
Binary Search for Insertion:
- In insertion sort, when using binary search to find the correct position for insertion, the worst case time complexity is O(N^2).
- This is because the binary search takes O(logN) time to find the correct position for insertion.
Insertion Time Complexity:
- Once the correct position is found using binary search, shifting all the elements to make space for insertion takes O(N) time.
- Thus, the total time complexity for insertion in this case is O(N) + O(logN) = O(N^2).
Overall Time Complexity:
- Since we have to find the correct position for each element in the array, the overall worst case time complexity of insertion sort using binary search is O(N^2).
Therefore, the worst case time complexity of insertion sort with binary search for insertion is O(N^2).