Classic sorting algorithm-comb sort

Source: Internet
Author: User

Classic sorting algorithm-comb sort

The sorting method is based on the Bubble sorting. Different from the Bubble sorting method, the sorting method compares and exchanges the number at a fixed distance, similar to that of Hill.

The fixed distance is the approximate value obtained by dividing the length of the array by 1.3. The next time the approximate value obtained is divided by 1.3, It is decreased by 1 until the distance is as small as 3.

Not very descriptive. Let's look at the example.

Suppose the array to be added [8 4 3 7 6 5 2 1]

The length of the array to be arranged is 8, and 8 bytes 1. 3 = 6, 8, 2, 4, and 1 are compared and exchanged.

[8 4 3 7 6 5 2 1]

[8 4 3 7 6 5 2 1]

The result after exchange is

[2 1 3 7 6 5 8 4]

In the second loop, the update interval is 6 Gb/s 1. 3 = 4, and 2, 6, 1, 5, 3, 8, 7, and 4 are compared.

[2 1 3 7 6 5 8 4]

[2 1 3 7 6 5 8 4]

[2 1 3 7 6 5 8 4]

[2 1 3 7 6 5 8 4]

Only 7 and 4 need to be exchanged, and the result after the exchange is

[2 1 3 4 6 5 8 7]

In the third loop, the update distance is 3 and there is no exchange

In the fourth loop, the update distance is 2 and there is no exchange

In the fifth cycle, the update distance is 1, and three switches

[2 13 4 6 5 8 7]

[2 1 3 46 58 7]

[2 1 3 4 6 58 7]

The result after the three exchanges is [1 23 45 6 7 8]

After the switch, the sorting is complete and the sequence output is [1 2 3 4 5 6 7 8].

 

 

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.