Classic sorting algorithm-comb sort comb sort

Source: Internet
Author: User

Classic sorting algorithm-comb sort comb sort

Comb sort or bubble-based sort, unlike bubbling, comb sorting compares and exchanges numbers at fixed distances, like Hill

This fixed distance is the array length divided by 1.3 to get an approximation, the next time the last obtained approximation is divided by 1.3, until the distance is small to 3 o'clock, decreasing by 1

It's not a good description, just look at the example.

Hypothetical Array [8 4 3 7 6 5 2 1]

The array length to be queued is 8, and 8÷1.3=6, then 8 and 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 the exchange is

[2 1 3 7 6 5 8 4]

Second cycle, update spacing for 6÷1.3=4, compare 2 and 6,1 and 5,3 and 8,7 and 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]

[2 1 3 7 6 5 8 4]

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

[2 1 3 4 6 5 8 7]

Third cycle, update distance of 3, no swap

Fourth cycle, update distance of 2, no swap

Fifth cycle, update distance of 1, three exchange

[2 1 3 4 6 5 8 7]

[2 1 3 4 6 5 8 7]

[2 1 3 4 6 5 8 7]

The result of the three exchanges is [1 2 3 4 5 6 7 8]

Post-swap sort ends, sequential output can be obtained [1 2 3 4 5 6 7 8]

Classic sorting algorithm-comb sort comb sort

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.