Python排序搜索基本算法之希尔排序实例分析
2017-12-09 11:28
891 查看
本文实例讲述了Python排序搜索基本算法之希尔排序。分享给大家供大家参考,具体如下:
希尔排序是插入排序的扩展,通过允许非相邻的元素进行交换来提高执行效率。希尔排序最关键的是选择步长,本程序选用Knuth在1969年提出的步长序列:1 4 13 40 121 364 1093 3280 。。。后一个元素是前一个元素*3+1,非常方便选取,而且效率还不错。代码如下:
#-*- coding: UTF-8 -*- def shellSort(seq): length=len(seq) inc=0 while inc<=length/3: inc=inc*3+1 print(inc) while inc>=1: for i in range(inc,length): tmp=seq[i] for j in range(i,0,-inc): if tmp<seq[j-inc]: seq[j]=seq[j-inc] else: j+=inc break seq[j-inc]=tmp inc//=3 if __name__=='__main__': print("脚本之家测试结果:") seq=[8,6,4,9,7,3,2,-4,0,-100,99] shellSort(seq) print(seq)
运行结果:
PS:这里再为大家推荐一款关于排序的演示工具供大家参考:
在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:
http://tools.jb51.net/aideddesign/paixu_ys
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- Python排序搜索基本算法之选择排序实例分析
- Python排序搜索基本算法之冒泡排序实例分析
- Python排序搜索基本算法之归并排序实例分析
- Python排序搜索基本算法之插入排序实例分析
- Python排序搜索基本算法之堆排序实例详解
- 【Python排序搜索基本算法】之希尔排序
- 【Python排序搜索基本算法】之Prim算法
- 【Python排序搜索基本算法】之Dijkstra算法
- 【Python排序搜索基本算法】之二叉树的遍历
- 【Python排序搜索基本算法】之无向图的最小割&Karger算法(Graphs and Minimum Cuts & Karger's Min-Cut Algorithm)
- 【Python排序搜索基本算法】之堆排序
- 【Python排序搜索基本算法】之深度优先搜索、广度优先搜索、拓扑排序、强联通&Kosaraju算法
- 【Python排序搜索基本算法】之拓扑排序
- 【Python排序搜索基本算法】之Prim算法
- 【Python排序搜索基本算法】之快速排序(QuickSort)
- 【Python排序搜索基本算法】之Dijkstra算法
- 【Python排序搜索基本算法】之插入排序
- 【Python排序搜索基本算法】之归并排序
- 【Python排序搜索基本算法】之选择排序
- 【Python排序搜索基本算法】之冒泡排序