Data Structures & AlgorithmsHardGATE CS · TCS NQT

What is the total number of comparisons made by insertion sort on the array [3, 1, 4, 1, 5] (each test of a[j-1] > a[j] counts as one comparison, and the shifting stops at the first failed test)?

  1. A. 7
  2. B. 6
  3. C. 8
  4. D. 10

Correct answer: B. 6

Each inner-loop test is one comparison. Insertion sort stops comparing as soon as the element is in place, so the count is below the worst case of 10.

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

Exams that ask this topic