我在递归版本中有快速排序算法:
def quick_sort_r(some_list):
less = []
equal = []
greater = []
if len(some_list) <= 1:
return some_list
else:
pivot = some_list[0]
for x in some_list:
if x < pivot:
less.append(x)
elif x > pivot:
greater.append(x)
else:
equal.append(x)
less = quick_sort_r(less)
greater = quick_sort_r(greater)
return greater + equal + less
def gen(number, some_list, b=100000):
"Generating list of random numbers".
for x in range(number):
x = n.random.randint(b, size=1)
some_list.extend(x)
x=x-1
domain = [10000, 25000, 50000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000]
for i in range(len(domain)):
print 'Results for: ' + str(domain[i]) + ' elements:'
for j in range(0,10):
list=[]
gen(dziedzina[i], list)
start = time.time()
quick_sort_r(list)
end = time.time() - start
print end
print '*************************'
是的,它应该是递归版本,是的,它应该是降序排序。
我当然有这个经典错误:
maximum recursion depth exceeded in cmp
为此域名生成此结果是否有任何可能的限制?我知道:
sys.setrecursionlimit(x)
答案 0 :(得分:0)
您的代码中存在一些可能导致递归问题的错误。
也有一些建议:
以下代码按预期工作,如果这是您想要的。
import random
import time
def quick_sort_r(some_list):
less = []
equal = []
greater = []
# print some_list
if len(some_list) <= 1:
return some_list
else:
pivot = some_list[0]
for x in some_list:
if x < pivot:
less.append(x)
elif x > pivot:
greater.append(x)
else:
equal.append(x)
less = quick_sort_r(less)
greater = quick_sort_r(greater)
return greater + equal + less
def gen(number, b=100000):
"Generating list of random numbers"
some_list = []
return [some_list.append(random.randint(0, b)) for x in xrange(number)]
domain = [10000, 25000, 50000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000]
for element in domain:
print 'Results for: ' + str(element) + ' elements:'
for j in range(0, 10):
temp_list = gen(element)
start = time.time()
quick_sort_r(temp_list)
end = time.time() - start
print end
print '*************************'