希尔&计数&基数排序

一、希尔排序

shell_sort

def insert_sort_gap(li,gap):
    for i in range(gap,len(li)):
        tem = li[i]  # 要插入的数
        j = i-gap # j指的是手里的牌的下标
        while li[j] > tem and j>=0:
            li[j+gap] = li[j]
            j -= gap
        li[j+gap] = tem

def shell_sort(li):
    d = len(li)//2
    while d>=1:
        insert_sort_gap(li,d)
        d//=2
import random
li = list(range(1000))
random.shuffle(li)
shell_sort(li)
print(li,'最后结果')

猜你喜欢

转载自www.cnblogs.com/foremostxl/p/10249114.html