In a permutation , of distinct integers, an inversion is a pair such that and
What would be the worst case time complexity of the Insertion Sort algorithm, if the inputs are restricted to permutations of with at most inversions?
View worked solution
Answer: D
The run time of the Insertion Sort algorithm is closely tied to the number of inversions in the input array.
Let be the number of elements in the array and be the number of inversions.
- In Insertion Sort, each swap of adjacent elements reduces the number of inversions by exactly . Therefore, the total number of shifts/swaps performed is exactly equal to the number of inversions .
- For each of the elements, the algorithm performs at least one comparison to determine its correct position (even if no swap is needed). Thus, the total number of comparisons is at most .
Consequently, the total time complexity of Insertion Sort is given by:
Given that the input permutations are restricted to at most inversions (i.e., ), we can substitute this upper bound into the complexity relation:
Thus, the worst-case time complexity under this restriction is .
Free practice · Your answers on this page are not saved.
