Data Structures & AlgorithmsHardGATE CS · ICPC regional

Which quantity gives the minimum number of arbitrary swaps needed to sort a permutation, and why is it correct?

  1. A. Number of inversions, because each swap removes exactly one inversion
  2. B. n minus the number of cycles, because each swap changes the cycle count by at most one
  3. C. Number of fixed points, because each swap fixes one element
  4. D. Sum of displacements divided by two, because swaps move two elements

Correct answer: B. n minus the number of cycles, because each swap changes the cycle count by at most one

A swap can split a cycle or merge two cycles, changing the cycle count by one; a sorted permutation has n cycles of length one. Inversions are only the answer for adjacent swaps.

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

Exams that ask this topic