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