Classic sorting algorithm-cycle sort

Source: Internet
Author: User

Classic sorting algorithm-cycle sort

The idea of cycle sort is too similar to that of Count sorting. It is helpful to understand the base sorting,

The difference between Ring sorting and counting sorting is that ring sorting only counts the numbers to be counted. Read the article first and then come back to understand this sentence.

The so-called circle definition can only be explained by examples.

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

 

Part 1

The first step is to observe the results of the array to be sorted and the index of the array to be sorted.

The 6 after sorting should appear at the position of index 4, but it is now at the position 0,

Remember this location. We always find a number. We should wait for 0 and our task will be completed.

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

Step 2, and 5 at the position 4 of the index to be sorted should appear at the position 3 of the index.

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

Step 3, similarly, the position of index 3 to be arranged in the array is, which should appear on the position 0. Note that if such a number is found: 1, it should be on the position 0.

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

Step 4, while there are 6 at index 0, and 6 should appear at index 4. Now we can find that, back to the origin, the problem is back to the first step,

So there is no such thing as step 4. The first three steps have already been completed.

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

This is the so-called circle! It's really hard to describe. I don't know if you understand it... Khan.

After the first three steps are completed, the data obtained is [6 5 1].

 

Part 2

In the first step, the circle sorting is not a circle, but a circle or multiple circles, so you have to continue searching. This step starts from the second digit 2.

2 In the waiting row is at Index 1, and the sorting is still at the position 1. Therefore, after this circle is completed, the circled data is obtained [2].

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

 

Part 3

Step 1: Same as above, 4 also shows the location where it should be waiting. End this lap and get the third lap: [4]

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

 

Part 4

In the first step, since 1 and 5 appear in the first circle, what does this mean? It means that these two numbers already have their own circles and you don't need to find them again,

In the end, we still get the data in the first lap [6 5 1]. Therefore, we can skip steps 1 and 5. This part is actually looking for 9. Let's take a look at 9 circles.

9 should appear at index 5. In fact, it is at index 5, which is the same as the first step in the second part. Therefore, the data in this circle also comes out: [9]

Array to be sorted [6 2 4 1 5 9]

After sorting [1 2 4 5 6 9]

Array Index [0 1 2 3 4 5]

A total of four circles are found, which are

[6 5 1], [2], [4], [9]

 

If a circle has only one number, it does not need to be circled, that is, it does not need to be sorted, so only the first circle can be sorted.

You may have to ask, the circles on the front can only be obtained based on the known sorting results, and I know that the results still have a margin.

The above content is to describe what is a circle, know what is a circle before you can understand the circle sorting.

Now let's break down the details of sorting.

The first step is to extract 6 and calculate that there are 4 digits smaller than 6, put 6 into index 4, and the number 5 at the original index 4 is listed

Before sorting [0 2 4 1 5 9] 6

[0 2 4 1 6 9] 5 after sorting

Index location [0 1 2 3 4 5]

 

Step 2: The current number 5 calculates the number with three digits smaller than 5, placing 5 in index 3, and the number at the same time the original index 3

Before sorting [0 2 4 1 6 9] 5

After sorting [0 2 4 5 6 9] 1

Index location [0 1 2 3 4 5]

 

Step 3: The current number 1 is calculated as follows: 0 digits are smaller than 1, 1 is put into index 0, and index 0 is blank. This circle is complete.

Before sorting [0 2 4 5 6 9] 1

After sorting [1 2 4 5 6 9]

Index location [0 1 2 3 4 5]

The first lap is completed in [6 5 1 ].

 

Step 4: Take the next number 2 and calculate that there is a number smaller than 2. Put 2 in index 1 and find that it is originally in index 1.

Step 5: Take the next number 4 and calculate that there are two numbers smaller than 4. Put 4 in index 2 and find that it is originally at index 2.

Step 6: extract the next number 5 and 5 in the first circle without sorting.

Step 7: retrieve the next number 6 and 6 in the first circle without sorting.

Step 8: Take the next number 9 and calculate that there are 5 digits smaller than 9. Put 9 in the index 5 and find that it is originally in the index 5.

All sorted

 

Back to main directory [classic Sorting Algorithm] [Collection]

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.