Data Structures & AlgorithmsModerateIBM · Deloitte pattern

How many element shifts does insertion sort perform to sort [4, 3, 2, 10, 12, 1, 5, 6] in ascending order?

  1. A. 12
  2. B. 13
  3. C. 8
  4. D. 15

Correct answer: A. 12

Each shift fixes exactly one inversion, so shifts = number of inversions = 3 (from 4) + 2 (from 3) + 1 (from 2) + 3 (from 10) + 3 (from 12) = 12. 8 is the number of elements, not of shifts.

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

Exams that ask this topic