我觉得。。。。取决于快排的算法是怎么写的。。。我这写得也太失败了。。
import time
import random
import uuid
def QuickSort(list):
result = list
sort_list = [[0, len(result) - 1]]
while sort_list:
start, end = sort_list.pop()
if start + 1 == end:
if result[start] > result[end]:
result[start], result[end] = result[end], result[start]
continue
pivot = result[start]
start = start
left_list = []
right_list = []
for i in range(start + 1, end + 1):
if result[i] < pivot:
left_list.append(result[i])
else:
right_list.append(result[i])
left_start = start
left_end = left_start + len(left_list) - 1
if left_start < left_end:
sort_list.append([left_start, left_end])
right_start = start + len(left_list) + 1
right_end = right_start + len(right_list) - 1
if right_start < right_end:
sort_list.append([right_start, right_end])
result = result[:start] + left_list + \
[pivot] + right_list + result[end + 1:]
return result
def comp(list):
t1 = time.time()
a1 = QuickSort(list)
t2 = time.time()
a2 = sorted(list)
t3 = time.time()
list.sort()
a3 = list
t4 = time.time()
print 'quick', t2 - t1, '\t',
print 'sorted', t3 - t2, '\t',
print 'sort', t4 - t3,
print a1 == a2 == a3
comp([random.random() for i in range(100000)])
comp([uuid.uuid1() for i in range(100000)])
comp([100000 - i for i in range(100000)])
quick 0.973999977112 sorted 0.00300002098083 sort 0.00300002098083 True
quick 57.228000164 sorted 0.00699996948242 sort 0.00600004196167 True
quick 13.3680000305 sorted 0.000999927520752 sort 0.0 True
[Finished in 72.0s]