简体   繁体   English

出现错误:比较中超出了最大递归深度

[英]Getting Error: Maximum Recursion Depth Exceeded in Comparison

I was trying to write the QuickSort algorithm using the Hoare Partition Scheme.我试图使用 Hoare 分区方案编写快速排序算法。 I'm pretty sure my Partition function is correct.我很确定我的分区 function 是正确的。 I use a variable 'Swaps' to indicate the movement of the left pivot towards the right and the movement of the right pivot towards the left.我使用变量“交换”来指示左侧 pivot 向右移动和右侧 pivot 向左移动。 The Sort function works with the other Partition algorithm so I think that's fine as well.排序 function 与其他分区算法一起使用,所以我认为这也很好。 Yet I get the error.然而我得到了错误。

inp=[2,3,6,3,9,7,8,0,5]

#Swap Function
def Swap(List, i, j):
    temp=List[i]
    List[i]=List[j]
    List[j]=temp


#QuickSort Function
def QSort(List, Start, End):
    if Start < End:

        PIEnd=Partition(List, Start, End)

        QSort(List,Start,PIEnd)
        QSort(List,PIEnd+1,End)

    return List



#Partition Function
def Partition (List, Start, End):
    Swaps=0
    PIStart=Start #PI = Pivot Index
    PIEnd=End 

    for i in range(Start, End):
        if List[PIStart] > List[PIEnd]:
            Swap(List, PIStart, PIEnd)
            Swaps=Swaps+1       
        if Swaps % 2 ==0:
            PIStart=PIStart+1
        else:
            PIEnd=PIEnd-1

    return PIEnd

print(QSort(inp, 0, 8))

Look in these two places...看看这两个地方...

# QSort ...
        PIEnd=Partition(List, Start, End)

        QSort(List,Start,PIEnd)
        QSort(List,PIEnd+1,End)

# Partition
        if Swaps % 2 ==0:
            PIStart=PIStart+1
        else:
            PIEnd=PIEnd-1

If you have an even quantity of swaps in any partition, then PIEnd will not change, and your indirect recursion in QSort will stick on the same arguments.如果您在任何分区中有偶数的交换,那么PIEnd不会改变,并且您在 QSort 中的间接递归将坚持相同的 arguments。 Your first low-half recursion does this.您的第一个低半递归执行此操作。 Revisit your logic.重新审视你的逻辑。 For starters, you should not depend on global variables for a solution.对于初学者,您应该依赖全局变量来解决问题。

Here's how I instrumented your code for a recursion trace:以下是我为递归跟踪检测您的代码的方法:

call_count = 0
indent = ""


#QuickSort Function
def QSort(List, Start, End):
    global call_count, indent
    indent += "  "
    call_count += 1
    print(indent, "ENTER QSort", Start, End, List)

    if call_count > 2 * len(inp):
        print(indent, "Too many calls")
        exit(1)

    if Start < End:

        PIEnd=Partition(List, Start, End)

        QSort(List,Start,PIEnd)
        QSort(List,PIEnd+1,End)

    print(indent, "ENTER QSort", Start, End, List)    
    indent = indent[2:]

    return List

Output: Output:

   ENTER QSort 0 8 [2, 3, 6, 3, 9, 7, 8, 0, 5]
     ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
       ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
         ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
           ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
             ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
               ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                 ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                   ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                     ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                       ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                         ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                           ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                             ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                               ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                                 ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                                   ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                                     ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                                       ENTER QSort 0 4 [2, 3, 0, 3, 5, 7, 8, 9, 6]
                                       Too many calls

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

相关问题 获取 RecursionError:比较超过最大递归深度 - Getting RecursionError: maximum recursion depth exceeded in comparison Fibonacci错误:相比之下超出了最大递归深度 - Fibonacci error: maximum recursion depth exceeded in comparison 在比较错误python中超过了最大递归深度? - Maximum recursion depth exceeded in comparison error python? 比较中是否超过了最大递归深度? - maximum recursion depth exceeded in comparison? 比较超过最大递归深度 - maximum recursion depth exceeded in comparison 为什么在此 Python 二进制搜索中出现“RecursionError:比较中超出最大递归深度”错误? - Why am i getting “RecursionError: maximum recursion depth exceeded in comparison” error on this Python Binary Search? Python错误:列表中的比较超出了最大递归深度 - Python Error: maximum recursion depth exceeded in comparison in list python编程错误RecursionError:比较超过最大递归深度 - python programming error RecursionError: maximum recursion depth exceeded in comparison 与循环参考相比,超出了最大递归深度 - maximum recursion depth exceeded in comparison with circular references RecursionError:在比较 cs 中超出了最大递归深度 - RecursionError: maximum recursion depth exceeded in comparison cs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM