Data Structures & AlgorithmsModerateGATE CS · Product placement

How many inversions (pairs i < j with a[i] > a[j]) does [5, 6, 10, 37, 38, 4, 17, 23] have? (This is also the number of swaps bubble sort makes.)

  1. A. 10
  2. B. 8
  3. C. 1
  4. D. 9

Correct answer: D. 9

Count, for each element, the smaller elements to its right; the total is 9. Counting only adjacent out-of-order pairs gives 1.

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

Exams that ask this topic