ProgrammingVery hard

Insertion sort is applied to a = {5, 2, 4, 6, 1, 3} and each element moved one slot to the right while shifting counts as one move. How many moves happen in total?

  1. A. 7
  2. B. 10
  3. C. 9
  4. D. 12

Correct answer: C. 9

The total equals the number of pairs (i < j) with a[i] > a[j], the inversions of the input. Each shift removes exactly one inversion.

More C Programming questions

Exams that ask this topic