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 ...
随机推荐
- Lucene基础学习笔记
在学校和老师一起做项目,在老师的推荐下深入学习了一些SqlServer的知识,看一些书下来哎也没记住多少,不过带来了新疑问. 不使用模糊查询,我应该用什么呢?如何能不影响数据库性能,还能做模糊查询呢? ...
- jquery click事件,多次执行
用jquery绑定一个按钮click事件后,第一次点击后,一切正常,第二次点击,竟然执行两次,以后越来越多, 后来查看文档发现 jquery click 不是 替换原有的function 而是接 ...
- .NET 获取Get方式URL中的参数键值
在Web开发中,我们常常会涉及到需要获取Get方式URL中的参数键值的情况,这里简单介绍三种方法: 第一种:常用的做法有使用JavaScript获取location.href后用正则表达式匹配获取此U ...
- 《HTTP权威指南》学习笔记——HTTP概述
1.HTTP--因特网的多媒体信使 HTTP使用的是可靠的数据传输协议,能确保数据在传输过程中不会损坏或混乱. 2.Web客户端和服务器 Web服务器(HTTP服务器)根据客户端的请求返回服务端的数据 ...
- Array常用函数收藏
1.isArray 语法:Array.isArray(obj) 说明:判断一个对象是否是数组. 例如: Array.isArray([]); Array.isArray([1]); Array.isA ...
- Go中error类型的nil值和nil
https://my.oschina.net/chai2010/blog/117923
- [原]unity3D bug记录
1.u3d 发出的xcode工程 崩溃出现这种信息 Display::DisplayLinkItem::dispatch() 到AppController.mm 修改成这样 - (void) Repa ...
- 屏蔽win10中文输入法
Windows 10自带的默认输入法,是通过Shift键来切换中/英文,这样为玩一些不需要打字,却需要使用Shift键的游戏带来了不便,比如,在进行游戏的时候,按下Shift键后,再按跳跃.射击等键, ...
- Android实现图片缩放示例
package com.example.demo; import android.os.Bundle; import android.app.Activity; import android.grap ...
- Splash scroll_position 属性
scroll_position属性用于控制页面上下或左右滚动,如下,表示控制页面向下滚动 400 像素值并返回结果图, function main(splash, args) assert(splas ...