标签:end 递归 alist high start low 排序 快速
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 |
标签:end,
递归,
alist,
high,
start,
low,
排序,
快速
From: https://blog.csdn.net/denghai_csdn/article/details/143199789