排序算法专题_1_GnomeSort (侏儒排序)——最简单的排序

最简单的排序算法不是冒泡排序…,不是插入排序…,而是Gnome排序!
The simplest sort algorithm is not Bubble Sort…, it is not Insertion Sort…, it’s Gnome Sort!

Gnome Sort is based on the technique used by the standard Dutch Garden Gnome (Du.: tuinkabouter). Here is 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 are 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 is done.

—Dick Grune

侏儒分类是基于标准荷兰花园侏儒(Du.:tuinkabouter)所使用的技术。以下是一个花园侏儒如何对一排花盆进行分类。基本上,他看旁边的花盆和前面的花盆;如果它们的顺序正确,他向前踩一个罐子,否则他交换它们,向后踩一个锅。边界条件:如果之前没有锅,他就向前走;如果他旁边没有锅,他就完了。

注意:侏儒排序是稳定排序算法
以下是最原始的侏儒排序的实现代码

C

void Gnome_Sort(int a[],int n){
    int i=0;
    while(i<n){
        if(i<0||a[i]<=a[i+1])i++;
        else {
            int t=a[i];
            a[i]=a[i+1];
            a[i+1]=t;
            i--;
        }
    }
}

C++

void Gnome_Sort(int a[],int n){
    int i=0;
    while(i<n){
        if(i<0||a[i]<=a[i+1])i++;
        else {
            swap(a[i],a[i+1]);
            i--;
        }
    }
}

The gnome sort may be optimized by introducing a variable to store the position before traversing back toward the beginning of the list. This would allow the “gnome” to teleport back to his previous position after moving a flower pot. With this optimization, the gnome sort would become a variant of the insertion sort.
—Dick Grune

gnome排序可以通过在返回列表的开头之前引入一个变量来存储位置来进行优化。这将允许“侏儒”在移动花盆后传送回他之前的位置。通过这种优化,gnome排序将成为插入排序的变体

这样就出现了GnomeSort的优化

void Gnome_Sort2(int a[],int n){
    int i=0;
    int last_i=-1;
    while(i<n){
        if(i<0||a[i]<=a[i+1]) {
            if(last_i==-1)
                i++;
            else i=last_i-1,last_i--;
        }
        else {
            if(last_i==-1)last_i=i;
            int t=a[i];
            a[i]=a[i+1];
            a[i+1]=t;
            i--;
        }
    }
}

如果再次优化,我们将可以得到插入排序的单循环版本(to be continue)

侏儒排序具体步骤流程图

题外话:
这里放一个可以用来测试排序的两个函数

随机数组函数:

//#include 
//#include  //头文件
void rand_array(int a[],int n){
    time_t t;
    srand((unsigned )time(&t));
    for(int i=0;i<n;i++){
        a[i]=rand()%100;
    }
}

测试函数

_Bool check(int a[],int n){
    for(int i=0;i<n-1;i++)if(a[i]>a[i+1])return 0;
    else return 1;
}

打印函数

void Print(int a[],int n){
    for(int i=0;i<n;i++)printf("%d ",a[i]);
    puts("");
}

你可能感兴趣的:(排序算法,算法,c++,c语言)