The usual implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the position, the worst case running time will
View worked solution
Answer: A
In a standard array-based implementation of Insertion Sort, inserting the -th element into a sorted subarray of size involves two main steps:
- Finding the correct insertion position: Using binary search, we can locate the correct position in time. Over iterations, the total search time is:
- Shifting the elements to make room: Since the elements are stored in a contiguous array, inserting an element at index requires shifting all elements from index to one position to the right. In the worst case (e.g., when the array is sorted in reverse order), we must shift all elements. Over iterations, the total shifting time is:
The total worst-case running time is the sum of the search time and the shifting time:
Thus, using binary search reduces the number of comparisons but does not reduce the number of element movements. The overall worst-case running time remains .
Free practice · Your answers on this page are not saved.
