zoukankan      html  css  js  c++  java
  • 排序算法(2):希尔排序

    希尔排序(有时称为“递减递增排序”)通过将原始列表分解为多个较小的子列表来改进插入排序,每个子列表使用插入排序进行排序。 选择这些子列表的方式是希尔排序的关键。不是将列表拆分为连续项的子列表,希尔排序使用增量i(有时称为 gap),通过选择 i 个项的所有项来创建子列表。

    以i=3为例,下图显示了三个子列表的插入排序过程:
    这里写图片描述
    虽然这个列表没有完全排序,但发生了很有趣的事情。 通过排序子列表,我们已将项目移动到更接近他们实际所属的位置。

    下图展示了使用增量为 1 的插入排序; 换句话说,标准插入排序。
    这里写图片描述

    注意,通过执行之前的子列表排序,我们减少了将列表置于其最终顺序所需的移位操作的总数。对于这种情况,我们只需要四次移位完成该过程。

    python实现代码如下:

    def shellSort(alist):
        sublistcount = len(alist)//2
        while sublistcount > 0:
    
          for startposition in range(sublistcount):
            gapInsertionSort(alist,startposition,sublistcount)
    
          print("After increments of size",sublistcount,
                                       "The list is",alist)
    
          sublistcount = sublistcount // 2
    
    def gapInsertionSort(alist,start,gap):
        for i in range(start+gap,len(alist),gap):
    
            currentvalue = alist[i]
            position = i
    
            while position>=gap and alist[position-gap]>currentvalue:
                alist[position]=alist[position-gap]
                position = position-gap
    
            alist[position]=currentvalue
    
    alist = [54,26,93,17,77,31,44,55,20]
    shellSort(alist)
    print(alist)

    乍一看,希尔排序不会比插入排序更好,因为它最后一步执行了完整的插入排序。 然而,结果是,该最终插入排序不需要进行非常多的比较(或移位),因为如上所述,该列表已经被较早的增量插入排序预排序。 换句话说,每个遍历产生比前一个“更有序”的列表。 这使得最终遍历非常有效。

    参考资料:《problem-solving-with-algorithms-and-data-structure-using-python》
    http://www.pythonworks.org/pythonds

  • 相关阅读:
    spring @resource @ Autowired
    mysql 。。。
    MYSQL
    oracle sql 性能 优化
    tomcat 解析(五)-Tomcat的核心组成和启动过程
    tomcat 解析(四)-处理http请求过程
    tomcat 解析(三)-启动框架
    tomcat 解析(二)-消息处理过程
    tomcat 解析(一)-文件解析
    我发起并创立了一个 C 语言编译器 开源项目 InnerC
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9411615.html
Copyright © 2011-2022 走看看