我正在做一个实验,看看什么是“最好的编程语言”,当我得到 1,000,000 个数字的字符串时,对于快速排序,它给了我这个错误:
Exception: maximum recursion depth exceeded in comparison.
我尝试使用
sys.setrecursionlimit(5000)
来增加可能的递归调用次数。但是这样做之后,它没有给我任何答案,它就结束了。
我用这个算法:
def partition(array, low, high):
# choose the rightmost element as pivot
pivot = array[high]
# pointer for greater element
i = low - 1
# traverse through all elements
# compare each element with pivot
for j in range(low, high):
if array[j] <= pivot:
# If element smaller than pivot is found
# swap it with the greater element pointed by i
i = i + 1
# Swapping element at i with element at j
(array[i], array[j]) = (array[j], array[i])
# Swap the pivot element with the greater element specified by i
(array[i + 1], array[high]) = (array[high], array[i + 1])
# Return the position from where partition is done
return i + 1
def quickSort(数组,低,高): 如果低< high:
# Find pivot element such that
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, low, high)
# Recursive call on the left of pivot
quickSort(array, low, pi - 1)
# Recursive call on the right of pivot
quickSort(array, pi + 1, high)
return array