Data Structures & AlgorithmsModerateCodeforces Div2 C · permutations

Problem: Which algorithm is the standard way to find the minimum number of swaps to sort a permutation?

  1. A. Count inversions only
  2. B. Dijkstra on positions
  3. C. Binary search on the answer
  4. D. Count cycles: swaps = n - (number of cycles)

Correct answer: D. Count cycles: swaps = n - (number of cycles)

Each cycle of length c needs c - 1 swaps, so the total is n minus the number of cycles.

More Coding Interview Problems questions

Exams that ask this topic