Data Structures & AlgorithmsModerateGATE CS · Product placement

How many inversions (pairs i < j with a[i] > a[j]) does [38, 33, 1, 17, 31, 28, 18, 35] have? (This is also the number of swaps bubble sort makes.)

  1. A. 14
  2. B. 16
  3. C. 4
  4. D. 15

Correct answer: D. 15

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

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

Exams that ask this topic