Skip to content
Official PYQ · 2003

GATE CS 2003 Q62 — Searching, Sorting, Hashing

Explain with AI

In a permutation a1…ana_1\ldots a_n, of nn distinct integers, an inversion is a pair (ai,aj)(a_i, a_j) such that i<ji < j and ai>aj.a_i > a_j.

What would be the worst case time complexity of the Insertion Sort algorithm, if the inputs are restricted to permutations of 1...n1. . . n with at most nn inversions?

Choose one option
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 nn be the number of elements in the array and II be the number of inversions.

  • In Insertion Sort, each swap of adjacent elements reduces the number of inversions by exactly 11. Therefore, the total number of shifts/swaps performed is exactly equal to the number of inversions II.
  • For each of the nn 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 I+nI + n.

Consequently, the total time complexity T(n)T(n) of Insertion Sort is given by:

T(n)=Θ(n+I)T(n) = \Theta(n + I)

Given that the input permutations are restricted to at most nn inversions (i.e., I≤nI \le n), we can substitute this upper bound into the complexity relation:

T(n)=Θ(n+n)=Θ(n)T(n) = \Theta(n + n) = \Theta(n)

Thus, the worst-case time complexity under this restriction is Θ(n)\Theta(n).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions