[英]Python- Getting a quicksort recursion error
I created a quicksort with a test case for the same array to n=2,4,...2^16.我为 n=2,4,...2^16 的相同数组创建了一个带有测试用例的快速排序。 My quicksort works (tested with a standard array), and now I'm trying to test for up to 2^16.
我的快速排序有效(使用标准数组测试),现在我尝试测试最多 2^16。 I am getting the following error:
我收到以下错误:
9 512
Single iteration for time elapsed: 0.0088467 seconds.
10 1024
10 1024
Traceback (most recent call last):
File "quicksort1.py", line 37, in <module>
quicksort1(array_to_sort, low, high)
File "quicksort1.py", line 12, in quicksort1
quicksort1(array, index + 1, high)
File "quicksort1.py", line 12, in quicksort1
quicksort1(array, index + 1, high)
File "quicksort1.py", line 12, in quicksort1
quicksort1(array, index + 1, high)
[Previous line repeated 993 more times]
File "quicksort1.py", line 10, in quicksort1
index = partition(array, low, high)
File "quicksort1.py", line 21, in partition
for i in range(low+1, high+1):
RecursionError: maximum recursion depth exceeded in comparison
As you can see, it runs to the 10th time then crashes.如您所见,它运行到第 10 次然后崩溃。 I used
import sys // sys.setrecursionlimit(20000)
but it did would run to the 15th iteration then crash again.我使用
import sys // sys.setrecursionlimit(20000)
但它确实会运行到第 15 次迭代然后再次崩溃。 If the limit was too high I would get a Segmentation fault: 11
.如果限制太高,我会得到一个
Segmentation fault: 11
。 Would anyone know how to approach this?有人知道如何解决这个问题吗? It just needs to run recursively without crashing.
它只需要递归运行而不会崩溃。 Thanks (code is below):
谢谢(代码如下):
#import sys
import random
import time
from random import randint
def quicksort1(array, low, high):
if high > low:
index = partition(array, low, high)
quicksort1(array, low, index - 1)
quicksort1(array, index + 1, high)
#sys.setrecursionlimit(20000)
def partition(array, low, high):
firstitem = array[low]
j = low
for i in range(low+1, high+1):
if array[i] < firstitem:
j+=1
array[j], array[i] = array[i], array[j]
index = j
array[low], array[index] = array[index], array[low]
return index
# testing our program
for k in range(1, 17):
unsorted_array = [random.randint(0, 2**k) for _ in range(2**k)]
time_start = time.clock()
for i in range(10):
array_to_sort = unsorted_array
print(k, len(array_to_sort))
low, mid, high = 0, len(array_to_sort)//2, len(array_to_sort)-1
quicksort1(array_to_sort, low, high)
time_elapsed = (time.clock() - time_start)
print("Single iteration for time elapsed: ", time_elapsed/10, "seconds.")
print("Time elapsed to run the program: ", time_elapsed, "seconds.")
You're only sorting the list on the first iteration.您只在第一次迭代时对列表进行排序。 The remaining 9 iterations are sorting the already sorted array.
剩下的 9 次迭代正在对已经排序的数组进行排序。
Since you are choosing the pivot to be the first item of the partition, you are seeing the worst case recursion for subsequent runs.由于您选择 pivot 作为分区的第一项,因此您会看到后续运行的最坏情况递归。
You should make a copy of the unsorted list like this:您应该像这样复制未排序的列表:
array_to_sort = unsorted_array[:]
Otherwise you are mutating the unsorted array.否则,您正在改变未排序的数组。
Note: there is still a small chance that you can hit the recursion limit if the random list is extremely unlucky.注意:如果随机列表非常不走运,您仍有可能达到递归限制。 This is a known issue with naive implementations of quicksort
这是快速排序的天真实现的一个已知问题
You can limit stack space to O(log(n)), but the worst case time complexity is still O(n^2), unless you improve the partitioning.您可以将堆栈空间限制为 O(log(n)),但最坏情况的时间复杂度仍然是 O(n^2),除非您改进分区。 Recursion is only used on the smaller partition, while iteration is used on the larger partition.
递归仅用于较小的分区,而迭代用于较大的分区。
def quicksort1(array, low, high):
while high > low:
index = partition(array, low, high)
if index-low < high - index:
quicksort1(array, low, index - 1)
low = index+1
else:
quicksort1(array, index + 1, high)
high - index-1
As already answered, for the sort testing, you need to randomize the array before each sort, or copy an already randomized array to a working array to be sorted.正如已经回答的那样,对于排序测试,您需要在每次排序之前对数组进行随机化,或者将已经随机化的数组复制到要排序的工作数组中。
Even ifound while using it on randomly generated numbers but after n=50000 onwards run time error showing similar message.即使我发现在随机生成的数字上使用它但在 n=50000 之后运行时错误显示类似的消息。
Anyone do reply with suitable solution任何人都会用合适的解决方案回复
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.