A brief talk on data structure (ii) Simple and interesting gnome sort

Source: Internet
Author: User

I have heard of the name of the Goblin sort very early, and today I see it as a very simple and interesting sorting algorithm.

Why is it called Goblin sort?

Goblin Sort in 2000 by Dr. Hamid Sarbazi-azad is called the stupid sort, which shows the simplicity of the idea. Later, the algorithm was described by Dick Grune as a gnome sort.

Story background:

Here's how a garden gnome sorts a line of flower pots.  Basically, he looks at the flower pot next to him and the previous one;  If they is in the right order he steps one pot forward, otherwise he swaps them and steps one pot backwards. Boundary Conditions:if There is no previous pot, he steps forwards; If there is no pot next to him, he's done.
-dick Grune

The idea is to have a goblin (a Dutch garden goblin Tuinkabouter https://nl.wikipedia.org/wiki/Tuinkabouter) in a row of disk. His arrangement from the front to the back, if the adjacent two discs were in the right order, he stepped forward; If the disk is in a wrong order, he steps back until all the discs are arranged in the order.

This is the Moe gnome (Tuinkabouter).

Algorithmic thinking

This algorithm can be seen as a simplified version of the bubbling sort . The idea of bubbling sorting is to constantly exchange inverse pairs, making the largest elements surface, while the goblin sort turns it back on when it finds the reverse-order pair, until it is sequenced, and then goes forward.

C + + implementation

The sort of goblin requires only one cycle , which is very simple and explains why it is called stupid at first.

voidGnomesort (intNintar[]) {    inti =0;  while(I <N) {if(i = =0|| Ar[i-1] <=Ar[i]) I++; Else         {            intTMP =Ar[i]; Ar[i]= Ar[i-1]; ar[I.] =tmp; }    }}

Note here that when I is 0 o'clock, the description goes back to the starting point and needs to move forward.

Optimization of the sorting of goblins

When the Goblin finds the inverse pair, it marks the current position with a stone and then swaps the reversed disk back. When the disk has found the correct position, the Goblin moves back to the stone marker position instantaneously. In this case, the goblin sort becomes an insertion sort.

Analysis of the sort of goblin

In the best case, the sequence is already lined up, the time complexity is O (n), the worst case, the sequence is reversed, the time complexity is O (n^2), and the bubble sort is the same.

A brief talk on data structure (ii) Simple and interesting 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.