Gnome排序(地精排序),起初由Hamid Sarbazi-Azad 于2000年提出,并被称为stupid排序,后来被Dick Grune描述并命名为“地精排序”,作为一个排序算法,和插入排序类似,除了移动一个元素到最终的位置,是通过交换一系列的元素实现,就像冒泡排序一样。概念上十分简单,不需要嵌套循环。时间复杂度为O,但是如果初始数列基本有序,时间复杂度将降为O(n)。实际上Gnome算法可以和插入排序算法一样快。平均运行时间为O.

Gnome排序算法总是查找最开始逆序的一对相邻数,并交换位置,基于交换两元素后将引入一个新的相邻逆序对,并没有假定当前位置之后的元素已经有序

Description

下面gnome排序算法的伪代码,使用0起始索引数组:

procedure gnomeSort(a[])
pos :=
while pos < length(a)
if (a[pos] >= a[pos-])
pos := pos +
else
swap a[pos] and a[pos-]
if (pos > )
pos := pos -
end if
end if
end while
end procedure

实例

给定一个无序数组, a = [5, 3, 2, 4], gnome排序将在while循环中执行如下的步骤.  "current position"采用加粗黑体:

Current array Action to take
[5, 3, 2, 4] a[pos] < a[pos-1], swap:
[3, 5, 2, 4] a[pos] >= a[pos-1], increment pos:
[3, 5, 2, 4] a[pos] < a[pos-1], swap and pos > 1, decrement pos:
[3, 2, 5, 4] a[pos] < a[pos-1], swap and pos <= 1, increment pos:
[2, 3, 5, 4] a[pos] >= a[pos-1], increment pos:
[2, 3, 5, 4] a[pos] < a[pos-1], swap and pos > 1, decrement pos:
[2, 3, 4, 5] a[pos] >= a[pos-1], increment pos:
[2, 3, 4, 5] a[pos] >= a[pos-1], increment pos:
[2, 3, 4, 5] pos == length(a), finished.

C代码如下:

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>
#include<time.h> void swap(int *a, int *b) //交换两元素的值
{
int t;
t=*a;
*a=*b;
*b=t;
} void printArray(int a[], int count) //打印数组元素
{
int i;
for(i=; i<count; i++)
printf("%d ",a[i]);
printf("\n");
} void gnome_sort(int *a, int len) //gnome排序算法
{
int pos=;
while(pos<len)
{
if(a[pos]>=a[pos-])
{
pos++;
}
else
{
swap(&a[pos],&a[pos-]);
if(pos>) pos--;
}
}
} int main(void)
{
int a[]={, , , , , , , , };
int n=sizeof(a)/sizeof(*a);
printArray(a,n);
gnome_sort(a,n);
printArray(a,n);
return ;
}

优化算法

gnome算法还可以通过引入一个变量,用于存储每次返回到数组前面的位置来进行优化。采用这样的优化,gnome排序将成为一个变种插入排序,下面优化后gnome排序算法的伪代码,使用0起始索引数组:

procedure optimizedGnomeSort(a[])
pos :=
last :=
while pos < length(a)
if (a[pos] >= a[pos-])
if (last != )
pos := last
last :=
end if
pos := pos +
else
swap a[pos] and a[pos-]
if (pos > )
if (last == )
last := pos
end if
pos := pos -
else
pos := pos +
end if
end if
end while
end procedure

C代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>
#include<time.h> void swap(int *a, int *b) //交换两元素的值
{
int t;
t=*a;
*a=*b;
*b=t;
} void printArray(int a[], int count) //打印数组元素
{
int i;
for(i=; i<count; i++)
printf("%d ",a[i]);
printf("\n");
} void optimizedGnome_Sort(int *a, int len) //优化后的gnome排序
{
int last,pos;
last=; pos=;
while(pos<len)
{
if(a[pos]>=a[pos-])
{
if(last!=)
{
pos=last;
last=;
}
pos++;
}
else
{
swap(&a[pos],&a[pos-]);
if(pos>)
{
if(last==)
last=pos;
pos--;
}
else
{
pos++;
}
}
}
} int main(void)
{
int a[]={, , , , , , , , };
int n=sizeof(a)/sizeof(*a);
printArray(a,n);
optimizedGnome_Sort(a,n);
printArray(a,n);
return ;
}

Gnome排序的更多相关文章

  1. Gnome排序算法

    Gnome排序(地精排序),起初由Hamid Sarbazi-Azad 于2000年提出,并被称为stupid排序,后来被Dick Grune描述并命名为“地精排序”,作为一个排序算法,和插入排序类似 ...

  2. 部分博文目录索引(C语言+算法)

    今天将本博客的部分文章建立一个索引,方便大家进行阅读,当然每一类别中的文章都会持续的添加和更新(PS:博文主要使用C语言) 博客地址:http://www.cnblogs.com/archimedes ...

  3. 数据结构杂谈(二)简单有趣的地精排序Gnome sort

    很早之前便听说过地精排序的名字,今天自己看来一下,发现这是一种非常简单而且有趣的排序算法. 为什么叫地精排序? 地精排序在2000年由Dr. Hamid Sarbazi-Azad 提出的时候被称作 s ...

  4. 地精排序(Gnome Sort) 算法

    gnome应该是最简单排序的排序算法吧!Gnome Sort,这是该算法的作者命名的,O(n*n)时间复杂度,O(1)空间复杂度,属于稳定的排序算法.算法的思想是每趟循环找到第一个逆序的元素,把它和在 ...

  5. 地精排序Gnome Sort

    号称最简单的排序算法,只有一层循环,默认情况下前进冒泡,一旦遇到冒泡的情况发生就往回冒,直到把这个数字放好为止 直接看它排序的过程,待排数组[6 2 4 1 5 9] 先设计一个标识i=0然后从头开始 ...

  6. 只有一重循环的排序——侏儒排序(Gnome Sort)

    侏儒排序:从头(i=0)开始遍历元素,如果当前元素比前一个元素大(array[i]>array[i-1]),就把它跟前一个元素互换(Swap(a[i],a[i-1]))并继续检查它(i--),否 ...

  7. 13个Cat命令管理(显示,排序,建立)文件实例

    在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...

  8. 【Unity3D自学记录】可视化对照十多种排序算法(C#版)

    在这篇文章中.我会向大家展示一些排序算法的可视化过程.我还写了一个工具.大家可对照查看某两种排序算法. 下载源代码 – 75.7 KB 下载演示样例 – 27.1 KB 引言 首先,我觉得是最重要的是 ...

  9. CodeForces - 138C: Mushroom Gnomes - 2 (线段树&概率&排序)

    One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her th ...

随机推荐

  1. jmm 和线程安全

    Java的内存模型JMM Java的内存模型JMM(Java Memory Model)JMM主要是为了规定了线程和内存之间的一些关系.根据JMM的设计,系统存在一个主内存(Main Memory), ...

  2. Single Pattern(单例模式)

    单例模式是一种常用的软件设计模式.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源.如果希望在系统中某个类的实例只能存在一个,单例模式是最好的 ...

  3. css只改变input输入框光标颜色不改变文字颜色实现方法

    input:focus{color:blue}//光标颜色 input{ text-shadow: 0px 0px 0px red;//文字颜色 -webkit-text-fill-color: tr ...

  4. Windows下使用最新的JDK1.7.0_51以上版本连接Jenkins出现SecurityException

    我在slave节点上安装了jdk1.8, 当在节点上启动slave-agent的时候,报安全性限制的错误: java.lang.SecurityException: Missing required ...

  5. CorelDRAW中如何再制对象详解

    再制对象指的是快捷地将对象按一定的方式复制为多个对象,此种复制是复制的复制,再制不仅可以节省复制的时间,再制间距还可以保证复制效果.本教程将详解如何在CorelDRAW软件中再制对象. CorelDR ...

  6. python tornado异步性能测试

    测试两个接口 # -*- coding:utf-8 -*- import time import tornado.web import tornado.gen import tornado.ioloo ...

  7. c#中如何退出程序后自动重新启动程序

    //触发退出程序事件 private void button1_Click(object sender, EventArgs e)        {             Application.E ...

  8. Maven -- 发布jar包至远程仓库

    啦啦啦

  9. SpringMVC -- 梗概--源码--壹--收参

    附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...

  10. Hightchart 技巧

    http://blog.csdn.net/u014796515/article/details/24428131