非递归快速排序

import random
def quick(alist):#非递归
        assert len(alist) >= 2
        q = []
        q.append((0,len(alist)-1))
        while q:
            start, end = q.pop()
            mid = alist[start]#mid is the pivot of the partition
            low = start
            high = end
            while low < high:
                while low < high and alist[high] >= mid:
                    high -= 1
                alist[low] = alist[high]
                while low < high and alist[low] < mid:
                    low += 1
                alist[high] = alist[low]
            alist[low] = mid
            if low - 1 > start:          
                q.append((start, low - 1))
            if low + 1 < end:
                q.append((low + 1, end))  
    x=random.sample(range(10000000),1500)
    x.sort()#排序后就是最坏情况,升降排序都一样(reverse=True)
    xx = x.copy()
    start = time.time()	    
    quick(xx)
    end = time.time()
    print("quick非递归",end - start)
    xquick = x.copy()
    start = time.time()	
    quick_sort(xquick,0,len(xquick) - 1)
    end = time.time()
    print("quick",end - start)                      
1500样本已排序
非递归0.12800002098083496
递归RecursionError: maximum recursion depth exceeded in comparison
1500样本未排序
非递归0.013000011444091797
递归0.006000041961669922
5000000样本未排序
非递归24.926000118255615
递归24.596999883651733
50000样本已排序
非递归126.83200001716614
递归RecursionError: maximum recursion depth exceeded in comparison

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值