The efficiency of the algorithm is not high. But it provides a good idea. How to make a sequence orderly with a minimum number of interchanges.
Cycle sort translated into Chinese is a circle sort.
The circle is the data formation circle that needs to be exchanged.
Specific point:
Such as:
Array 4 3 2 5 5 6 arrays to process
Result 2 3 4 5 5 6 results
POS 0 1 2 3 4 5 subscript
Starting from the elements of subscript 0. 4 required to subscript 2 and subscript 2 for the element 2 need to subscript 0. Just can go back to 4. Which is the formation of 4-2 of such a circle
Next is the 3 need to subscript 1 and 3 itself on 1. Then 3 comes in a circle.
And then the next 2. This 2 is in Circle 1, so skip.
Then is 5 5 needed on 3 (maybe you think 4 is also possible.) But in fact, we are sort of similar counting methods. Algorithm is stable) 5 in a circle
The next 5 is the same.
And the next 6 is the same.
In actual operation:
We start at 4 with a similar count sort of position. Add a step. Determine if the element at the current position should be positioned at 4. If not. Whether or not the element to which this element should be placed will continue to be accessed should be placed in the 4 position. Until you can.
And then it's going to loop.
Specifically or see the summary of this blog is good. Much more understood.
Cycle sort (lowest number of exchanges)