python数据结构之希尔排序
来源:互联网 发布:西门子plm软件下载 编辑:程序博客网 时间:2024/05/16 02:38
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 LL = [5,4,2,3,6,1,0]print("原始序列:")print(L)print("希尔排序:")print(ShellSort(L))
程序输出结果:
原始序列:[5, 4, 2, 3, 6, 1, 0]希尔排序:step = 3i=3 i-step=03<-->5[3, 4, 2, 5, 6, 1, 0]i=5 i-step=21<-->2[3, 4, 1, 5, 6, 2, 0]i=6 i-step=30<-->5[3, 4, 1, 0, 6, 2, 5]i=3 i-step=00<-->3[0, 4, 1, 3, 6, 2, 5]step = 1i=2 i-step=11<-->4[0, 1, 4, 3, 6, 2, 5]i=3 i-step=23<-->4[0, 1, 3, 4, 6, 2, 5]i=5 i-step=42<-->6[0, 1, 3, 4, 2, 6, 5]i=4 i-step=32<-->4[0, 1, 3, 2, 4, 6, 5]i=3 i-step=22<-->3[0, 1, 2, 3, 4, 6, 5]i=6 i-step=55<-->6[0, 1, 2, 3, 4, 5, 6][0, 1, 2, 3, 4, 5, 6]
阅读全文
0 0
- python数据结构之希尔排序
- 数据结构之希尔排序
- 数据结构之希尔排序
- 数据结构之希尔排序
- 数据结构之希尔排序
- 数据结构--排序之希尔排序
- 数据结构之排序:希尔排序
- 数据结构学习之 希尔排序
- 数据结构之排序之希尔排序
- 【希尔排序】数据结构实验之排序六:希尔排序
- 【数据结构之排序3】希尔排序
- 数据结构之插入排序与希尔排序
- 数据结构之排序(四)希尔排序
- 数据结构实验之排序六:希尔排序
- 数据结构实验之排序六:希尔排序
- 数据结构实验之排序六:希尔排序
- java数据结构排序之希尔排序实现
- 数据结构实验之排序六:希尔排序
- 如何在软件中实现多camera模组的兼容
- SQL SERVER ON LINUX安装和使用
- Shiro 的Permissions
- python print的简单用法
- makefile静态模式
- python数据结构之希尔排序
- iOS GCD定时器的使用
- springboot项目修改之后不需要重启的方法
- (四)spring cloud云服务架构
- 使用tf-slim的inception_resnet_v2预训练模型进行图像分类
- javascript -- 密码强度的验证
- linux 基础
- jquery each函数参数传递的问题
- Spring 下载教程 【最新版Spring官网】