Data Structures & AlgorithmsModerateTCS Digital · Infosys SP

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

  1. A. 24
  2. B. 25
  3. C. 6
  4. D. 23

Correct answer: A. 24

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

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

Exams that ask this topic