Gnome排序
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排序的更多相关文章
- Gnome排序算法
Gnome排序(地精排序),起初由Hamid Sarbazi-Azad 于2000年提出,并被称为stupid排序,后来被Dick Grune描述并命名为“地精排序”,作为一个排序算法,和插入排序类似 ...
- 部分博文目录索引(C语言+算法)
今天将本博客的部分文章建立一个索引,方便大家进行阅读,当然每一类别中的文章都会持续的添加和更新(PS:博文主要使用C语言) 博客地址:http://www.cnblogs.com/archimedes ...
- 数据结构杂谈(二)简单有趣的地精排序Gnome sort
很早之前便听说过地精排序的名字,今天自己看来一下,发现这是一种非常简单而且有趣的排序算法. 为什么叫地精排序? 地精排序在2000年由Dr. Hamid Sarbazi-Azad 提出的时候被称作 s ...
- 地精排序(Gnome Sort) 算法
gnome应该是最简单排序的排序算法吧!Gnome Sort,这是该算法的作者命名的,O(n*n)时间复杂度,O(1)空间复杂度,属于稳定的排序算法.算法的思想是每趟循环找到第一个逆序的元素,把它和在 ...
- 地精排序Gnome Sort
号称最简单的排序算法,只有一层循环,默认情况下前进冒泡,一旦遇到冒泡的情况发生就往回冒,直到把这个数字放好为止 直接看它排序的过程,待排数组[6 2 4 1 5 9] 先设计一个标识i=0然后从头开始 ...
- 只有一重循环的排序——侏儒排序(Gnome Sort)
侏儒排序:从头(i=0)开始遍历元素,如果当前元素比前一个元素大(array[i]>array[i-1]),就把它跟前一个元素互换(Swap(a[i],a[i-1]))并继续检查它(i--),否 ...
- 13个Cat命令管理(显示,排序,建立)文件实例
在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...
- 【Unity3D自学记录】可视化对照十多种排序算法(C#版)
在这篇文章中.我会向大家展示一些排序算法的可视化过程.我还写了一个工具.大家可对照查看某两种排序算法. 下载源代码 – 75.7 KB 下载演示样例 – 27.1 KB 引言 首先,我觉得是最重要的是 ...
- 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 ...
随机推荐
- C语言中的数组问题
数组默认最后一位是 结束符 占一位, 假如是7个字节大小的数组 实际输入为6个字节,最后一个字节为'\0' 这样写 char password_set[7]={"123456"}; ...
- scala 日期格式转换
scala> val format = new java.text.SimpleDateFormat("dd-MM-yyyy") 注意MM必须要大写 format: java ...
- Gridview中的选择、删除、编辑、更新、取消留着备用。
后台程序: public partial class tw2 : System.Web.UI.Page{ protected void Page_Load(object sender, Even ...
- 升级到yosemite后homebrew报错的解决
报错会如下: /usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Ve ...
- Jenkins入门知识
1 修改jenkins的根目录,默认地在C:\Documents and Settings\AAA\.jenkins . .jenkins ├─jobs│ └─JavaHelloWorld│ ...
- windows 下为Python安装redis
最近在看<redis实战>,里面的redis示例都是Python写的,先将环境整好 启动redis redis-server.exe redis.conf 安装了python2.7 安装 ...
- BarTender中每个标签提示手动输入的设置方法
我们知道手动输入数据进行标签打印,可以利用BarTender中的数据输入表单来实现.表单的创建有利于提示程序员,输入要打印标签的的数据.如果手动输入数据的标签较多,可以设置表单提示为每个标签提示,减去 ...
- linux下安装软件的常用方法
在使用Linux系统的过程中,软件包的安装是避免不了的,在Linux下,软件安装程序的种类很多,安装方法也各式各样,(舒适性自然比不上windows :-))不过我们常见的软件包有两种: 1)含有软件 ...
- LINE@生活圈招募好友秘笈
什么是「获得更多好友」页面? 您可从 LINE@ app >管理>获得更多好友 进入此页面. ▼ 「获得更多好友」新介面中,募集好友的四大秘诀 秘诀一.「以社群网站或电子邮件分享」 • ...
- mysql中显示当前数据库下的所有表,包括视图。
环境说明: mysql版本:5.5.57-log 操作系统:Red Hat Enterprise Linux Server release 6.6 (Santiago) 需求:查看当前数据库下所有的表 ...