Compute the minimal number of swaps to order a sequence
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The minimal number of swaps depends on what kind of swaps are allowed. If any two positions may be swapped, the answer comes from cycle decomposition of the permutation. If only adjacent swaps are allowed, the answer is the inversion count. Those are different problems, and mixing them is the most common source of confusion.
First decide which swap model you mean
These two questions are not the same:
- minimum number of arbitrary swaps to sort the sequence
- minimum number of adjacent swaps to sort the sequence
For example, for [4, 3, 2, 1]:
- arbitrary swaps need only
2 - adjacent swaps need
6
So the first step is choosing the correct problem model.
Arbitrary swaps: use cycle decomposition
If any two positions can be swapped, sort the values conceptually and track where each element needs to go. The permutation breaks into cycles, and a cycle of length k needs exactly k - 1 swaps.
Example implementation:
This solves the arbitrary-swap version efficiently.
Adjacent swaps: count inversions
If only neighboring elements can be swapped, the minimum number of swaps equals the number of inversions:
- a pair
(i, j)such thati < jbutnums[i] > nums[j]
A simple but slow implementation is:
For large inputs, you would normally count inversions in O(n log n) with a merge-sort-based approach.
Duplicates need careful handling
When duplicates exist, "the" sorted target may not correspond to one unique permutation. The cycle-decomposition approach still works, but you need a stable and well-defined mapping from original positions to sorted positions.
That is why the indexed-pairs approach above stores original indices explicitly instead of assuming values are unique.
Why cycle decomposition works
Suppose an element belongs in another position, and that position's element belongs somewhere else, and so on until the chain returns to the start. That closed chain is a cycle.
A cycle of length k can be fixed in k - 1 arbitrary swaps because each swap places one additional element into its final position.
This is the key insight behind the minimal arbitrary-swap formula.
Choose the algorithm by the allowed operation
Use:
- cycle decomposition for arbitrary swaps
- inversion counting for adjacent swaps
If you confuse the two, you may get an answer that is mathematically correct for the wrong problem.
Common Pitfalls
- Forgetting to specify whether swaps may be arbitrary or only adjacent.
- Using cycle decomposition when the real problem only allows adjacent swaps.
- Assuming duplicates do not affect the mapping from current order to sorted order.
- Computing a minimal arbitrary-swap count and comparing it to a bubble-sort swap count.
- Treating all "swap to sort" problems as one interchangeable algorithmic task.
Summary
- The minimum swap count depends on which swaps are allowed.
- Arbitrary swaps are solved by cycle decomposition.
- Adjacent swaps are solved by inversion counting.
- Duplicates require careful mapping to sorted positions.
- Most confusion in this problem comes from mixing two different swap models.

