Goblin Sorted Gnome Sort

Source: Internet
Author: User

Known as the simplest sort algorithm, there is only one layer of loops, and by default it bubbles forward, and when it happens, it goes back, until the number is set.

The process of looking directly at it, the array to be sorted [6 2 4 1 5 9]

First design an identity i=0 and then start from scratch, when (I < 6) is not set, when the sort ends,

So, how to control the value of I is the key to this algorithm

For example, an array to be queued:

[6 2 4 1 5 9]

[0 1 2 3 4 5]

Take a look at the specific sequencing process

[i = 0] do nothing at all, let I self-increment 1, reached a value of 1 to start a real comparison

Before Exchange [6 2 4 1 5 9][i = 0]

After Exchange [6 2 4 1 5 9][i = 1]

[i = 1] Compare 6 and 2, exchange occurs, as long as the interchange I is reduced by 1

Before Exchange [6 2 4 1 5 9][i = 1]

After Exchange [2 6 4 1 5 9][i = 0]

[i = 0] again 0, nothing to do, self-increase into 1.

Before Exchange [2 6 4 1 5 9][i = 0]

After Exchange [2 6 4 1 5 9][i = 1]

[i = 1] again compare 2 and 6, do not exchange, as long as do not change on the self-increase 1

Before Exchange [2 6 4 1 5 9][i = 1]

After Exchange [2 6 4 1 5 9][i = 2]

[i = 2] Compare 6 and 4, exchange occurs, as long as the exchange is reduced by 1

Before Exchange [2 6 4 1 5 9][i = 2]

After Exchange [2 4 6 1 5 9][i = 1]

[i = 1] Compare 2 and 4, do not exchange, as long as not exchange on the self-increment 1

Before Exchange [2 4 6 1 5 9][i = 1]

After Exchange [2 4 6 1 5 9][i = 2]

[i = 2] Compare 4 and 6, do not exchange, as long as not exchange on the self-increment 1

Before Exchange [2 4 6 1 5 9][i = 2]

After Exchange [2 4 6 1 5 9][i = 3]

[i = 3] Compare 6 and 1, exchange, as long as the exchange is reduced by 1

Before Exchange [2 4 6 1 5 9][i = 3]

After Exchange [2 4 1 6 5 9][i = 2]

[i = 2] Compare 4 and 1, exchange, as long as the exchange is reduced by 1

Before Exchange [2 4 1 6 5 9][i = 2]

After Exchange [2 1 4 6 5 9][i = 1]

[i = 1] Compare 2 and 1, exchange, as long as the exchange is reduced by 1

Before Exchange [2 1 4 6 5 9][i = 1]

After Exchange [1 2 4 6 5 9][i = 0]

[i = 0] do nothing at all, let I self-increment 1, reached a value of 1 to start a real comparison

Before Exchange [1 2 4 6 5 9][i = 0]

After Exchange [1 2 4 6 5 9][i = 1]

[i = 1] Compare 1 and 2, do not exchange, as long as not exchange on the self-increment 1

[i = 2] Compare 2 and 4, do not exchange, as long as not exchange on the self-increment 1

[i = 3] Compare 4 and 6, do not exchange, as long as not exchange on the self-increment 1

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

Before Exchange [1 2 4 6 5 9][i = 4]

After Exchange [1 2 4 5 6 9][i = 3]

[i = 3] Compare 4 and 5, do not exchange, as long as not exchange on the self-increment 1

[i = 4] Compare 5 and 6, do not exchange, as long as not exchange on the self-increment 1

[i = 5] Compare 6 and 9, do not exchange, as long as not exchange on the self-increment 1

[i = 6] expression (I < n) is not true, sort ends,

Sequential output results: [1 2 4 5 6 9]

The following code is for reference only

        static void Gnome_sort (int[] unsorted)        {            int i = 0;            while (I < unsorted. Length)            {                if (i = = 0 | | unsorted[i-1] <= unsorted[i])                {                    i++;                }                else                {                    int tmp = unsorted[i];                    Unsorted[i] = unsorted[i-1];                    UNSORTED[I-1] = tmp;                    i--;}}}        

Goblin Sorted Gnome 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.