如果我想通过以下QuickSort算法进行排序,则会收到“ RecursionError:在比较中超出最大递归深度”失败,或者如果我将递归限制设置得更高,则Python在通过BubbleSort使用(Process进行排序后,将停止工作以退出代码-1073741571(0xC00000FD)完成。有谁知道我该如何解决这个问题
设置更高的递归限制
import time
import random
from sys import setrecursionlimit
#setrecursionlimit(1000000)
#------------------------------Sortieralgorithmen-----------------------------------------------
#-----------------------------------------------------------------------------------------------
def bubbleSort(array):
length = len(array)
for sorted in range(length):
for j in range(0, length-sorted-1):
if array[j] > array[j+1] :
array[j], array[j+1] = array[j+1], array[j]
def help(array, low, high):
pivot = array[low]
fromhigh = high
fromlow = low
while True:
while(array[fromhigh]>pivot):
fromhigh = fromhigh-1
while(array[fromlow]<pivot):
fromlow = fromlow+1
if(fromlow<fromhigh):
array[fromlow], array[fromhigh] = array[fromhigh], array[fromlow]
else:
return fromhigh
def quickSort(array, low, high):
if (low<high):
pivot = help(array, low, high)
quickSort(array, low, pivot)
quickSort(array, pivot + 1, high)
#--------------------------------Zeitmessung-----------------------------------------------------
#------------------------------------------------------------------------------------------------
numb_elements = 6000
sortedarray = []
for i in range (0,numb_elements):
sortedarray.append(i)
notsortedarray = random.sample(range(0,numb_elements),numb_elements)
copy1 = sortedarray.copy()
before = time.time()
bubbleSort(copy1)
after = time.time()
total = after-before
print("Bubblesort sortiertes Array:\t\t" + str(total))
copy2 = notsortedarray.copy()
before = time.time()
bubbleSort(copy2)
after = time.time()
total = after-before
print("Bubblesort nicht sortiertes Array:\t" + str(total))
copy3 = sortedarray.copy()
before = time.time()
quickSort(copy3, 0, len(copy3)-1)
after = time.time()
total = after-before
print("QuickSort sortiertes Array:\t\t\t" + str(total))
copy4 = notsortedarray.copy()
before = time.time()
quickSort(copy4, 0, len(copy4)-1)
after = time.time()
total = after-before
print("QuickSort nicht sortiertes Array:\t" + str(total)+"\t (Worst Case)")
答案 0 :(得分:1)
一些介绍性跟踪显示了该问题:
indent = ""
def quickSort(array, low, high):
global indent
# print(indent, "quickSort ENTER", low, high)
indent += " "
if (low<high):
pivot = help(array, low, high)
print(low, high, pivot)
quickSort(array, low, pivot)
quickSort(array, pivot + 1, high)
indent = indent[2:]
看到堆栈问题后,我插入了包含数据透视表的打印语句。输出是
0 5999 0
1 5999 1
2 5999 2
3 5999 3
4 5999 4
5 5999 5
6 5999 6
...
994 5999 994
995 5999 995
996 5999 996
Traceback (most recent call last):
File "so.py", line 79, in <module>
quickSort(copy3, 0, len(copy3)-1)
File "so.py", line 46, in quickSort
quickSort(array, pivot + 1, high)
File "so.py", line 46, in quickSort
quickSort(array, pivot + 1, high)
File "so.py", line 46, in quickSort
quickSort(array, pivot + 1, high)
[Previous line repeated 994 more times]
File "so.py", line 43, in quickSort
pivot = help(array, low, high)
File "so.py", line 28, in help
while(array[fromhigh]>pivot):
RecursionError: maximum recursion depth exceeded in comparison
您的问题是您的枢纽逻辑总是返回低元素,这会将您的算法更改为 O(N ^ 2),当N
为大于允许的堆栈深度。
可以从那里拿走吗?
请参阅这个可爱的debug博客以获取帮助。
即使是琐碎的print
也会给您很多线索。