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

妖精的绣舞 提交于 2019-12-24 00:52:31

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33199185/how-do-you-find-the-minimal-set-of-swaps-that-will-sort-a-known-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!