简体   繁体   English

MergeSort递归实现

[英]MergeSort recursive implementation

I just learned Python today, and am trying to recursively implement Mergesort... I absolutely cannot figure out what I am doing wrong. 我今天才刚刚学习Python,并且正在尝试递归实现Mergesort ...我绝对无法弄清楚我在做什么错。 Can someone help? 有人可以帮忙吗?

def mergesort(A):

    if len(A) < 2:
        return A
    middle = len(A) // 2
    L = mergesort(A[:middle])
    R = mergesort(A[middle:])
    return merge(L, R)

# Merge - combine part of mergesort

def merge(Lsort, Rsort):


sort = [None] * (len(Lsort + Rsort))


i = 0
j = 0
k = 0 
while (len(Lsort) <= len(sort)) or (len(Rsort) <= len(sort)):
    if Lsort[i] < Rsort[j]:
        sort[k] = Lsort[i]
        i += 1
    else:
        sort[k] = Rsort[j]
        j += 1
    k += 1
return sort

It is nothing to do with Python, but rather with the logic. 它与Python无关,而与逻辑无关。

Change while expression to while表达时更改为

while k < len(sort):

and if expression to if表达为

if (j >= len(Rsort)) or (i < len(Lsort) and Lsort[i] < Rsort[j]):

Your merge function is the problem, should be something more like: 您的合并功能是问题,应该更像是:

def merge(Lsort, Rsort):                                                                                                                                                                                    
    sort = []                                                                                                                                                                                               

    i = 0                                                                                                                                                                                                   
    j = 0                                                                                                                                                                                                   
    while (len(Lsort) > i) & (len(Rsort) > j):                                                                                                                                                              
        if Lsort[i] < Rsort[j]:                                                                                                                                                                             
            sort.append(Lsort[i])                                                                                                                                                                           
            i += 1                                                                                                                                                                                          
        else:                                                                                                                                                                                               
            sort.append(Rsort[j])                                                                                                                                                                           
            j += 1                                                                                                                                                                                          

    if len(Lsort) > 0:                                                                                                                                                                                      
        sort += Lsort[i:]                                                                                                                                                                                   

    if len(Rsort) > 0:                                                                                                                                                                                      
        sort += Rsort[j:]                                                                                                                                                                                   

    return sort

In general, you don't need to allocate space for sort just append to an empty list as you go. 通常,您无需分配空间来进行排序,只需随便添加到一个空列表即可。 For the while logic you need to make sure you don't exceed the bounds of the Lsort and Rsort arrays. 对于一段时间的逻辑,您需要确保不要超出Lsort和Rsort数组的范围。 Make sure your indices are less the array length. 确保索引小于数组长度。

When either array is exhausted you may concatenate the remaining array items to your sorted list. 当两个数组中的任何一个都用尽时,您可以将其余数组项连接到已排序列表中。

Here is the Python version provided by http://interactivepython.org/runestone/static/pythonds/SortSearch/TheMergeSort.html 这是http://interactivepython.org/runestone/static/pythonds/SortSearch/TheMergeSort.html提供的Python版本。

(note below I pasted an image of the flow of an application on a sample list that I made to help me understand the recursive flow (as I am a nube)). (请注意,我在下面的示例列表中粘贴了应用程序流程的图像,以帮助我理解递归流程(因为我是一个小人))。

MergeSortPython代码

MergeSort流示例

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

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