GATE Computer ScienceModerateGATE CS · ISRO

How many inversions (pairs i < j with A[i] > A[j]) does the array [8, 4, 2, 1, 7, 3, 6, 5] have?

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

Correct answer: B. 15

Counting the smaller elements to the right of each element gives 15 inversions, which is also the number of adjacent swaps bubble sort needs.

More Algorithms questions

Exams that ask this topic