1. 程式人生 > >希爾&計數&基數排序

希爾&計數&基數排序

一、希爾排序

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,'最後結果')