Algorithm Chapter---Circle sort cycle

Source: Internet
Author: User

Classic sorting algorithm-Cycle sort

The so-called circle of definition, I can only think of examples to illustrate, really bad description

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

The first part

In the first step, we now look at the array and the results after the row, as well as the index of the array to be queued, to find

6 of the sequence should appear at index 4, and it is now on position 0,

Remember this location, always find a number should stay in position 0 our mission is done.

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

The second step, and 5 in the array index 4 position should appear in the position of index 3

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

The third step, the same, the position of the array index 3 is that it should appear in position 0, note that a number has been found: 1, it should stay in position 0

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

The fourth step, but the index 0 is placed 6, and 6 should appear in the index 4 position, so you can find, back to the original point, the problem back to the first step,

So there's no fourth step here, and the first three steps have already turned around.

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

This is the so-called lap! Really bad description, I do not know you see ... Sweat.

The first three steps to complete a lap, the data obtained are [6 5 1]

Part II

In the first step, the circle sort is not a round order, but one or more laps, so you have to keep looking, this step starts at the second number 2.

The 2 in the queue is at index 1, and the sort is still in position 1, so this lap is complete, get the loop data [2]

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

Part III

The first step, ibid. 4 also appeared where it should be, ending this lap and getting a third lap: [4]

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

Fourth part

The first step, because 1 and 5 appear in the first lap, what does this mean, that the two numbers already have their own circle, no longer looking for,

That is to find, and finally to get the first lap of data [6 5 1], so 1 and 5 skip, this part actually should be looking for 9, to see the circle of 9

9 should appear in the position of index 5, in fact it is in the position of index 5, and the first step of the second part of the same situation, so this lap of data also came out: [9]

arrays to be queued [6 2 4 1 5 9]

After sequencing [1 2 4 5 6 9]

array index [0 1 2 3 4 5]

A total of four circles were found, respectively

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

If a circle has only one number, then it does not need to be in circles, that is, it does not need to be sorted, then only the first lap is sorted

You may have to ask, the circles in front of them are based on the known sort results, and I know the result is still a hair.

All of these are meant to illustrate what a circle is, and to know what a circle can be before it is well understood circle sort

Now to break down the sorting details

The first step, take 6 out, calculated that there are 4 numbers smaller than 6, 6 into index 4, and the original index 4 position of the number 5 dequeue

Before sorting [0 2 4 1 5 9] 6

After sorting [0 2 4 1 6 9] 5

index position [0 1 2 3 4 5]

The second step, the current number 5, calculated that there are 3 numbers smaller than 5, 5 into index 3, and the original index 3 position of the number

Before sorting [0 2 4 1 6 9] 5

After sorting [0 2 4 5 6 9] 1

index position [0 1 2 3 4 5]

The third step, the current number 1, calculated that there are 0 numbers smaller than 1, put 1 in index 0, index 0 is empty, this loop is complete

Before sorting [0 2 4 5 6 9] 1

After sorting [1 2 4 5 6 9]

index position [0 1 2 3 4 5]

First lap [6 5 1] Complete

Fourth step, take out the next number 2, calculated that there are 1 numbers smaller than 2, put 2 in index 1, found that it was at index 1

Fifth step, take out the next number 4, calculated that there are 2 numbers smaller than 4, put 4 in index 2, found that it was at index 2

Sixth step, take out the next digit 5,5 in the first circle, do not need to sort

Seventh step, take out the next digit 6,6 in the first circle, do not need to sort

Eighth step, take out the next number 9, calculated that there are 5 numbers smaller than 9, put 9 in index 5, found that it was at index 5

Sort all finished

This article transferred from: http://www.cnblogs.com/kkun/archive/2011/11/28/2266559.html

Algorithm Chapter---Circle sort cycle

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.