python希尔排序的优缺点_希尔排序(使用Python描述)

问题描述

个人理解希尔排序就是插入排序的改进版,理解希尔排序之前需要先理解插入排序的逻辑.

此算法特性

暂为空,待补充

此算法适用场景

暂为空,待补充

代码实现

def shellSort(alist):

list_count = len(alist) // 2 # 分为多少份.每次除以2.最低是1(while做限制).

while list_count > 0: # 做限制

for start_position in range(list_count): # 分块

getInsertionSort(alist,start_position,list_count) # 使用插入排序函数

list_count = list_count // 2

def getInsertionSort(alist,start,gap): # 插入排序抽象

for i in range(start+gap,len(alist),gap): # start+gap 是起始对比下标

current_value = alist[i]

now_index = i

while now_index>=gap and alist[now_index-gap] > current_value:

# now_index>=gap是做一个限制.

alist[now_index] = alist[now_index-gap]

now_index = now_index - gap

alist[now_index] = current_value

alist = [21,152,33,3241,54,654,423,12,32]

shellSort(alist)

print(alist)

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值