zoukankan      html  css  js  c++  java
  • 地精排序(Gnome Sort) 算法

    gnome应该是最简单排序的排序算法吧!Gnome Sort,这是该算法的作者命名的,O(n*n)时间复杂度,O(1)空间复杂度,属于稳定的排序算法。算法的思想是每趟循环找到第一个逆序的元素,把它和在它前面的已排序的元素逐个进行比较、交换,有点像插入排序。

    void gnomeSort(int a[],int len)
    {
        int i=1;
        while(i<len)
        {
            if(a[i-1]<=a[i])
            {
                i++;
            }
            else
            {
                int tmp=a[i-1];a[i-1]=a[i];a[i]=tmp;
                i--;
            }
        }
    }

    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 (该算法发明人的话)

    该算法优化,wikipedia有说明:http://en.wikipedia.org/wiki/Gnome_sort

    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. The animation in the introduction to this topic takes advantage of this optimization.

    Here is pseudocode for an optimized gnome sort using a zero-based array:

    算法优化 gnome2:地精在回头检查花盘顺序前记下当前位置,整理完就瞬间转移到之前位置的下一个位置。瞬间转移,很强大。

    void gnomeSortOpt(int a[],int len)
    {
        int i=1;
        int prevPosition=-1;
        while(i<len)
        {
            if(a[i-1]<=a[i])
            {
                if(prevPosition==-1)
                {
                    i++;
                }
                else
                {
    // After traverse backward, go to the position next to the previous i
    =prevPosition+1; prevPosition=-1; } } else { if(prevPosition==-1) //记住现在的位置 Mark the Gnome's previous position before traverse backward { prevPosition=i; } int tmp=a[i-1];a[i-1]=a[i];a[i]=tmp; i--; } } }

    Gnome sort version 2类似插入排序,不断把列表后面待排序的元素插入到列表前面已排序的部分中,但类似于冒泡排序那样比较并交换相邻元素。因此其算法复杂度与插入排序相当,但比插入排序需要更多的复制操作(用于交换相邻元素)。

    Gnome sort version 3,搬了无数花盘后,地精想到一个新方法。如果交换相邻两个花盘,每次要搬花盘三次(复制三次),如果先拿起来当前位置的花盘(temp=data[i]),就有空位给其他花盘移动,每次只搬一次花盘(复制一次,data[i]=data[i-1])。这次地精先拿一个花盘在手(temp),如果手中花盘(temp)应放到上一个花盘(i-1)前面,就用脚(地精一般不高,脚够长吗?^_^)将上一个花盘(i-1)踢到当前位置(i),然后拿着花盘(temp)走到上一个位置(i=i-1),重复此过程。

     
    void gnomeSortOpt2(int a[],int len)
    {
        int i=1;
        int prevPosition=-1;
        int tmp;
        while(i<len)
        {
            if(a[i-1]<=a[i])
            {
                if(prevPosition==-1)
                {
                    i++;
                }
                else
                {
                    a[i]=tmp;
                    i=prevPosition+1;
                    prevPosition=-1;
                }
                tmp=a[i];
            }
            else
            {
                if(prevPosition==-1)
                {
                    prevPosition=i;
                }
                
                a[i]=a[i-1];
    
                i--;
            }
        }
    }

    Gnome sort version 3已经和插入排序一样了,呵呵。

    地精排序最显著的特点是代码只有一层循环,原版算法代码极短,但效率不高;改进后相当于插入排序的变种,不过条件判断多了,代码比标准的插入排序长。
    以类似方式,选择排序,冒泡排序也可以改成一层循环。
    在简单的基于比较的排序中(不包括快速排序,归并排序,堆排序等),插入排序总体性能不错,建议作为首选;而选择排序有最少的元素复制次数,当元素复制代价较大时(如存储介质的写入速度比读取速度慢很多)是个好选择。
    另外,只做部分的选择排序,就成了选择第n小元素的选择算法。正如部分的快速排序成了quickselect选择算法。

    http://en.wikipedia.org/wiki/Selection_algorithm

    转自:http://blog.csdn.net/winark/article/details/5918944

     

  • 相关阅读:
    git或gitee 提交代码到远程仓库
    gitee 创建代码仓库,并提交本地代码
    Logback 实现日志链路追踪
    navicat 查看,设计并导出数据库 ER图
    Jackson 使用 @JsonFormat 注解进行时间格式化
    Redis 缓存常见问题
    jedis 与 redission 实现分布式锁
    Redis 集群模式搭建
    Redis 哨兵模式高可用
    Notepad++ 正则表达式提取信息
  • 原文地址:https://www.cnblogs.com/youxin/p/3268640.html
Copyright © 2011-2022 走看看