Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我已经用python实现了mergesort,但它给了我一个;比较中超过了最大递归深度“;错误_Python 3.x_Sorting_Recursion_Mergesort - Fatal编程技术网

Python 3.x 我已经用python实现了mergesort,但它给了我一个;比较中超过了最大递归深度“;错误

Python 3.x 我已经用python实现了mergesort,但它给了我一个;比较中超过了最大递归深度“;错误,python-3.x,sorting,recursion,mergesort,Python 3.x,Sorting,Recursion,Mergesort,我曾尝试在python中实现mergesort,但在“mergesort”函数中,我检查排序是否完成,它给出的最大递归深度超过了比较错误。我知道这意味着它重复了太多的功能,但我不知道是什么导致它或如何修复它。我尝试过调整一些设置,但似乎没有任何效果。下面提供了代码和错误 def mergesort(arr, start, end): if start > end: return middle = (start + end) // 2 merges

我曾尝试在python中实现mergesort,但在“mergesort”函数中,我检查排序是否完成,它给出的最大递归深度超过了比较错误。我知道这意味着它重复了太多的功能,但我不知道是什么导致它或如何修复它。我尝试过调整一些设置,但似乎没有任何效果。下面提供了代码和错误

def mergesort(arr, start, end):
    if start > end:
        return

    middle = (start + end) // 2

    mergesort(arr, start, middle)
    mergesort(arr, middle + 1, end)

    return merge(arr, start, end, middle)

def merge(arr, start, end, middle):
    leftCopy = arr[start:middle]
    rightCopy = arr[middle + 1:end]

    leftIndex = 0
    rightIndex = 0

    sortedIndex = start

    while leftIndex < len(leftCopy) and rightIndex < len(rightCopy):
        if leftCopy[leftIndex] > rightCopy[rightIndex]:
            arr[sortedIndex] = leftCopy[leftIndex]
            leftIndex += 1
        else:
            arr[sortedIndex] = rightCopy[rightIndex]
            rightIndex += 1

        sortedIndex += 1

    while leftIndex < len(leftCopy):
        arr[sortedIndex] = leftCopy[leftIndex]
        leftIndex += 1
        sortedIndex += 1

    while rightIndex < len(rightCopy):
        arr[sortedIndex] = rightCopy[rightIndex]
        rightIndex += 1
        sortedIndex += 1

sample = [3, 1, 2]

mergesort(sample, 0, len(sample) - 1)
print(sample)
def合并排序(arr、开始、结束):
如果开始>结束:
返回
中间=(开始+结束)//2
合并排序(arr、start、middle)
合并排序(arr,中间+1,结束)
返回合并(arr、开始、结束、中间)
def合并(arr、开始、结束、中间):
leftCopy=arr[开始:中间]
rightCopy=arr[中间+1:结束]
leftIndex=0
rightIndex=0
sortedIndex=开始
当leftIndexrightCopy[rightIndex]:
arr[sortedIndex]=leftCopy[leftIndex]
leftIndex+=1
其他:
arr[sortedIndex]=rightCopy[rightIndex]
rightIndex+=1
sortedIndex+=1
当leftIndex
错误:

Traceback (most recent call last):
  File "mergesort.py", line 43, in <module>
    mergesort(sample, 0, len(sample) - 1)
  File "mergesort.py", line 7, in mergesort
    mergesort(arr, start, middle)
  File "mergesort.py", line 7, in mergesort
    mergesort(arr, start, middle)
  File "mergesort.py", line 7, in mergesort
    mergesort(arr, start, middle)
  [Previous line repeated 995 more times]
  File "mergesort.py", line 2, in mergesort
    if start > end:
RecursionError: maximum recursion depth exceeded in comparison
回溯(最近一次呼叫最后一次):
文件“mergesort.py”,第43行,在
合并排序(示例,0,len(示例)-1)
文件“mergesort.py”,第7行,在mergesort中
合并排序(arr、start、middle)
文件“mergesort.py”,第7行,在mergesort中
合并排序(arr、start、middle)
文件“mergesort.py”,第7行,在mergesort中
合并排序(arr、start、middle)
[上一行重复了995次]
文件“mergesort.py”,第2行,在mergesort中
如果开始>结束:
RecursionError:比较中超出了最大递归深度

开始==结束时,您的基本情况不包括在内(它应该是这样的:单元素列表不能无序);在这种情况下,
middle
=
start
,因此第一个递归调用与当前调用相同——因此是无休止的递归

start==end
时会发生什么情况?我尝试使用start==end或start。您是否验证了
merge
是否正常工作?我认为该函数有问题,但我无法确定它是什么。我刚刚意识到我不小心把“>”改为“好的,谢谢,你能告诉我如何解决这个问题吗?如果我检查start==end,列表根本没有排序,或者(看起来是这样的)是随机排序的,所以我认为merge函数中也有问题,但我不知道它是什么。EDIT:所以结果我弄错了if语句并使用了“>”而不是