简体   繁体   English

在Python中合并排序-RuntimeError:超过了最大递归深度

[英]Merge Sort in Python - RuntimeError: maximum recursion depth exceeded

I am working on merge sort in Python. 我正在使用Python进行合并排序。

I have checked merge function. 我已经检查了merge功能。 The arrays are merging fine. 数组合并很好。

But in the mergeSort Function an error is coming :--- 但是在mergeSort函数中出现错误:---

RuntimeError: maximum recursion depth exceeded. RuntimeError:超过最大递归深度。

RuntimeError Traceback (most recent call last) in () 63 print(arr[i]), 64 ---> 65 mergeSort(arr,0,n-1) 66 print(" ") 67 print("Sorted Array is") ()中的RuntimeError Traceback(最近一次调用最后一次)在()63 print(arr [i]),64 ---> 65 mergeSort(arr,0,n-1)66 print(“”)67 print(“ Sorted Array is” )

in mergeSort(arr, l, r) 53 m = l+(r-1)/2 54 mergeSort(arr,l,m) ---> 55 mergeSort(arr,m+1,r) 56 merge(arr,l,m,r) 57 在mergeSort(arr,l,r)中53 m = l +(r-1)/ 2 54 mergeSort(arr,l,m)-> 55 mergeSort(arr,m + 1,r)56 merge(arr,l ,m,r)57

What can be the possible cause of this? 这可能是什么原因?

def merge(arr,l,m,r):
    n1 = m-l+1
    n2 = r-m

    L = [0] * n1
    R = [0] * n2

    print("First List")
    for i in range(0,n1):
        L[i] = arr[i+l]
        print(L[i]), 


    print("")
    print("Second List")    
    for j in range(0,n2):
        R[j] = arr[j+m+1]
        print(R[j]),



    #Merging the temp arrays
    i = 0
    j = 0
    k = 0
    print("")
    print("Merged List ---------->")

    while i < n1 and j < n2:
        if L[i] <= R[j]:
            arr[k] = L[i]
            print(arr[k]),
            i+=1
        else:
            arr[k] = R[j]
            print(arr[k]),
            j+=1
        k+=1

    while i<n1:
        arr[k] = L[i]
        i+=1
        k+=1

    while j<n2:
        arr[k] = R[j]
        print(arr[k])
        j+=1
        k+=1

def mergeSort(arr,l,r):
    if l<r:
        m = l+(r-1)/2
        mergeSort(arr,l,m)
        mergeSort(arr,m+1,r)
        merge(arr,l,m,r)


arr = [0,12,13,0,1,22]
n = len(arr)

mergeSort(arr,0,n-1)
print(" ")
print("Sorted Array is")
for i in range(0,n-1):
    print(arr[i])

The way m is calculated in mergeSort is wrong(you need to divide in half the whole expression, not only (r-1) ). mergeSort计算m的方法是错误的(您需要将整个表达式除以一半,而不仅是(r-1) )。 Change it to: 更改为:

m = (l+(r-1))/2

As you were calculating that incorrectly your method was calling itself recursively over and over again until the maximum method stack depth was exceeded and thus crashing. 当您计算错误时,您的方法会一次又一次地递归调用自身,直到超出最大方法堆栈深度并因此崩溃。

  • You did the basic mistake .Python is dynamically type language 您犯了一个基本错误.Python是动态类型语言
  • Before going to why you got that Run Time RecurssionError ,you need to make one change. 在继续说明为什么出现Run Time RecurssionError之前,您需要进行一次更改。
    Instead of m = l+(r-1)/2 write m = (l+r)/2 代替m = l+(r-1)/2m = (l+r)/2
    Because when you called mergeSort(arr,0,n-1) at that time n-1 is last index value and that's why there is no need of r-1 in m = l+(r-1)/2 因为当您在那时调用mergeSort(arr,0,n-1)时, n-1是最后一个索引值,因此在m = l+(r-1)/2不需要r-1

Floor Division operator(//) VS Normal division operator(/) 楼层划分运算符(//)VS普通划分运算符(/)

Now coming to the main point why did you got that RecurssionError here is the answer 现在说到要点,为什么会出现RecurssionError就是答案

  • When you are doing operation like 当你做像

m = (l+r)/2 m =(l + r)/ 2

Divide operation will give fractional part ie m will be treated as floating variable 除法运算将得到小数部分,即m将被视为浮动变量
so m would never be 0 it would be any floating number say something 0.123 or 2.122 or 1.0025 所以m永远不会为0 ,它将是任何浮点数,例如0.1232.1221.0025

Because of floating point as m would never be 0 the if condition if l<r: would always true and the mergeSort() function goes into Infinite loop and that's why you got that RunTime RecurssionERROR 由于浮点数,因为m永远不会为0,所以如果条件if l<r:始终为true,并且mergeSort()函数进入无限循环 ,这就是为什么您得到RunTime RecurssionERROR

You want m as integer and not float so instead of m=(l+r)/2 您希望将m作为整数而不是浮点数 ,而不是m =(l + r)/ 2

Write m=(l+r)//2 the floor division operator(//) will give you integer value and your mergeSort() function will not go under Infinite Loop 写入m =(l + r)// 2 地板除法运算符(//)将为您提供整数值,并且您的mergeSort()函数将不会进入无限循环

Your code will get executed this time without any Error just make one change m=(l+r)//2 这次您的代码将被执行而没有任何错误,只需进行一次更改即可m =(l + r)// 2

BUT BUT BUT your merge() function algo is not good it does not give sorted array also the data is lost and something else gets printed 但是但是您的merge()函数算法仍然不好,它没有给出排序的数组,数据也丢失了,其他东西被打印了

Its because when you are calling mergesort function, you are passing l = 0 and r= lenght of list. 这是因为在调用mergesort函数时,传递的是l = 0和r = list的长度。 And no matter what your calculation going to be in every case you l will be less than r. 而且无论在每种情况下计算结果如何,您的l都会小于r。

 m = l+(r-1)/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM