Data Structures & AlgorithmsHardGATE CS · Product placement

Insertion sort is run on an array of n elements that has exactly k inversions. What is its running time?

  1. A. Θ(nk)
  2. B. Θ(n²) regardless of k
  3. C. Θ(n log n)
  4. D. Θ(n + k)

Correct answer: D. Θ(n + k)

Each shift removes exactly one inversion and each of the n − 1 insertions does at most one extra comparison, so the work is Θ(n + k) — linear for nearly sorted data.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic