Cycle sort (lowest number of exchanges)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.