GATE Computer ScienceHardGATE CS (PYQ pattern)

For the array [8, 3, 10, 1, 7, 5, 12, 2, 9, 4, 6], let X be the number of exchanges bubble sort performs and Y the minimum number of swaps of arbitrary pairs of elements needed to sort it. What is X − Y?

  1. A. 20
  2. B. 25
  3. C. 19
  4. D. 28

Correct answer: A. 20

Bubble sort performs one exchange per inversion: X = 28. Y = n − (number of cycles of the permutation) = 11 − 3 = 8. So X − Y = 20. Subtracting the number of cycles instead gives 25.

More Algorithms questions

Exams that ask this topic