Classic sorting algorithm-gnome sort

Source: Internet
Author: User

Classic sorting algorithm-gnome sort

It is called the simplest sorting algorithm. There is only one loop. By default, the algorithm moves forward and bubbles. Once the bubble occurs, it goes back until the number is properly placed.

Directly look at the sorting process, the array to be sorted [6 2 4 1 5 9]

First, design a logo I = 0 and then judge from the beginning, when (I <6) is not true, and when the sorting ends,

So,How to control the I value is the key to this algorithm

For example:

[6 2 4 1 5 9]

[0 1 2 3 4 5]

 

Take a look at the specific sorting process

[I = 0] When I do not do anything, first let I increase by 1, to reach the value of 1 to start the real comparison

Before switching [6 2 4 1 5 9] [I = 0]

[6 2 4 1 5 9] [I = 1] After exchange

 

[I = 1] compare 6 and 2. If exchange occurs, 1 is subtracted if I is exchanged.

Before interchange [6 24 1 5 9] [I = 1]

After Exchange [2 64 1 5 9] [I = 0]

 

[I = 0] It's 0 again, so I don't want to do anything. Let's say that auto-increment is 1.

Before switching [2 6 4 1 5 9] [I = 0]

[2 6 4 1 5 9] [I = 1] After exchange

 

[I = 1] compare 2 and 6 again, do not exchange, as long as do not change auto increment 1

Before interchange [2 6 4 1 5 9] [I = 1]

After the switch [2 6 4 1 5 9] [I = 2]

 

[I = 2] compare 6 and 4. If an exchange occurs, 1 is subtracted as long as the exchange occurs.

Before interchange [26 41 5 9] [I = 2]

After Exchange [24 61 5 9] [I = 1]

 

[I = 1] compare 2 and 4, do not exchange, as long as there is no exchange, auto increment 1

Before interchange [2 46 1 5 9] [I = 1]

After Exchange [2 46 1 5 9] [I = 2]

 

[I = 2] compare 4 and 6. If there is no exchange, the auto-increment value is 1 as long as there is no exchange.

Before interchange [24 61 5 9] [I = 2]

After Exchange [24 61 5 9] [I = 3]

 

[I = 3] compare 6 and 1, exchange, as long as the exchange is reduced by 1

Before interchange [2 46 15 9] [I = 3]

After Exchange [2 41 65 9] [I = 2]

 

[I = 2] compare 4 and 1, exchange, as long as the exchange is reduced by 1

Before interchange [24 16 5 9] [I = 2]

After Exchange [21 46 5 9] [I = 1]

 

[I = 1] compare 2 and 1, exchange, as long as the exchange is reduced by 1

Before interchange [2 14 6 5 9] [I = 1]

After Exchange [1 24 6 5 9] [I = 0]

 

[I = 0] When I do not do anything, first let I increase by 1, to reach the value of 1 to start the real comparison

Before switching [1 2 4 6 5 9] [I = 0]

[1 2 4 6 5 9] [I = 1] After exchange

[I = 1] compare 1 and 2, do not exchange, as long as there is no exchange, auto increment 1

[I = 2] compare 2 and 4. If there is no exchange, the auto-increment value is 1 as long as there is no exchange.

[I = 3] compare 4 and 6. If there is no exchange, the auto-increment value is 1 as long as there is no exchange.

[I = 4] compare 6 and 5, exchange, as long as the exchange is reduced by 1

Before interchange [1 2 46 59] [I = 4]

After Exchange [1 2 45 69] [I = 3]

[I = 3] compare 4 and 5. If there is no exchange, the auto-increment value is 1 as long as there is no exchange.

[I = 4] compare 5 and 6. If there is no exchange, the auto-increment value is 1 as long as there is no exchange.

[I = 5] compare 6 and 9, do not exchange, as long as do not exchange auto increment 1

[I = 6] The expression (I <n) is not true and the sorting ends,

Output results in sequence: [1 2 4 5 6 9]

Void gnomesort (int n, int ar []) {

Int I = 0;

While (I <n ){

If (I = 0 || ar [I-1] <= ar [I]) I ++;

Else {int TMP = ar [I]; Ar [I] = ar [I-1]; Ar [-- I] = TMP ;}

}

}

Reference http://blog.csdn.net/winark/article/details/5918944

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.