Sedgewick增量希尔排序和快速排序 (附Python代码)

比对结果

随机数组长度为 10000时:

Time for heer is 0.4159576892852783
Time for quick is 0.07650184631347656

随机数组长度为 100000时:

Time for heer is 4.3795859813690186
Time for quick is 0.89105224609375

Sedgewick增量序列的希尔排序

def heerSort(ar):
    Sedgewick = [1, 5, 19, 41, 109, 209, 505, 929, 2161,
                   3905, 8929, 16001, 36289, 64769, 146305,
                   260609, 587521, 1045505, 2354689, 4188161,
                   9427969, 16764929, 37730305, 67884289,
                   150958081, 268386305, 603906049, 1073643521]
    i = 0
    keeper = 0
    
    while i < 28 :
    
        if Sedgewick[i] > len(ar):
            keeper = i - 1
            break
            
        i += 1
        
    while keeper >= 0:
        i = Sedgewick[keeper]
        j = i
        
        while j < len(ar):
            p = j
            temp = ar[p]

            while p > 0 and ar[p - i] > temp:
                ar[p] = ar[p - i]
                p -= i
                
            ar[p] = temp
            j += 1
            
        keeper -= 1

快速排序

如果运行报错,请尝试加入

import sys
sys.setrecursionlimit(9000000) 
def quickSort(ar, left, right):

    if left >= right:
        return

    low = left
    high = right
    base = ar[low]

    while left < right:

        while left < right and ar[right] >= base:
            right -= 1

        ar[left] = ar[right]

        while left < right and ar[left] <= base:
            left += 1

        ar[right] = ar[left]

    ar[right] = base
    quickSort(ar, low, left - 1)
    quickSort(ar, left + 1, high)
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值