Search code examples
algorithmsortingdata-structureslanguage-agnostic

How do you find the minimal set of swaps that will sort a known list?


It is known that the problem of sorting an unknown lists can't be done in less than N * log(N) steps in average. But what about the problem of finding the optimal sorting for a known list? That is, suppose you have the following list:

[1,3,2,7,4]

On that case, only 2 swaps leave it sorted:

swap 1 2
swap 3 4

Which is much less than 5 * log 5. How do I find the minimal set of swaps that will leave a specific list sorted?

Note: this question is very similar to my previous one, except without stack machines.


Solution

  • This question becomes much easier once you turn the permutation into a cycle decomposition.

    For your example, the cycle decomposition using zero-based indices is (0)(2 1)(4 3). Each cycle of length k will require k-1 swaps to place into the correct order, so the answer to the minimal set of swaps is the sum of (cycle length - 1) for each cycle, and the exact set of swaps is determined from identifying the cycles and switching each element in the cycle with the next one in the cycle.

    The complexity of this approach is O(nlogn) to find the rank of each element plus O(n) to find the cycle decomposition.

    This answer assumes you can swap an arbitrary pair of elements.

    If you can only swap adjacent elements you need to compute the number of inversions in the array, see counting inversions.