Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP

Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the key or at the start) sorts [56, 48, 42, 34, 11, 45, 20, 8]. How many element shifts and how many key comparisons does it make?

  1. A. 24 shifts, 31 comparisons
  2. B. 25 shifts, 26 comparisons
  3. C. 24 shifts, 24 comparisons
  4. D. 24 shifts, 26 comparisons

Correct answer: D. 24 shifts, 26 comparisons

Shifts equal the number of inversions (24). Each insertion makes one extra comparison that stops the loop unless the key travels to index 0, so comparisons = 26.

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

Exams that ask this topic