python数据结构之希尔排序
2017-09-27 09:32
176 查看
python数据结构之希尔排序
#-*-coding:utf-8-*- ''' 将序列划分为两部分,将这两部分依次比较,若前大后小,则交换。 将步长除以2(向下取整),直到步长=0,依次比较。 ''' def ShellSort(L): step = len(L)//2 # 设定步长,Python2则用/ while step > 0: print('step = ' + repr(step)) for i in range(step, len(L)): while i >= step and L[i-step] > L[i]: print('i=' + repr(i) + ' i-step=' + repr(i-step)) print(repr(L[i]) + '<-->' + repr(L[i-step])) L[i], L[i-step] = L[i-step], L[i] print(L) i -= step step = step//2 return L L = [5,4,2,3,6,1,0] print("原始序列:") print(L) print("希尔排序:") print(ShellSort(L))
程序输出结果:
原始序列: [5, 4, 2, 3, 6, 1, 0] 希尔排序: step = 3 i=3 i-step=0 3<-->5 [3, 4, 2, 5, 6, 1, 0] i=5 i-step=2 1<-->2 [3, 4, 1, 5, 6, 2, 0] i=6 i-step=3 0<-->5 [3, 4, 1, 0, 6, 2, 5] i=3 i-step=0 0<-->3 [0, 4, 1, 3, 6, 2, 5] step = 1 i=2 i-step=1 1<-->4 [0, 1, 4, 3, 6, 2, 5] i=3 i-step=2 3<-->4 [0, 1, 3, 4, 6, 2, 5] i=5 i-step=4 2<-->6 [0, 1, 3, 4, 2, 6, 5] i=4 i-step=3 2<-->4 [0, 1, 3, 2, 4, 6, 5] i=3 i-step=2 2<-->3 [0, 1, 2, 3, 4, 6, 5] i=6 i-step=5 5<-->6 [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6]
相关文章推荐
- python数据结构之希尔排序
- Python中列表、字典、元组、集合数据结构整理
- Python和数据结构学习 --- 5
- Python中列表、字典、元组数据结构的简单学习笔记
- python 数据结构 元组、列表、字典、序列
- Python常见数据结构详解
- [从头学数学] 第259节 Python实现数据结构:平衡多路搜索树(B-Tree)
- 2.Python调用C语言之常用变量与数据结构
- 希尔排序 - 数据结构和算法91
- python数据结构学习笔记-2016-10-24-01-排序列表
- 第8节--python数据结构--tuple
- Python常用数据结构描述
- Python数据结构之——list
- 【数据结构】树与二叉树 基础要点+二叉树 Python 实现
- Python数据结构(列表、字典、集合、元组)详细解析
- Python一日一练14----希尔排序
- Python随身听基础篇03-基本数据结构list和dict
- Python中的高级数据结构
- Python常用数据结构--列表
- python 2.7 中文教程-5:数据结构