Python 归并排序算法

Python Merge Sort Algorithm

您好,我正在尝试制作一个合并排序算法来取乐,我不想只是从 Internet 上复制代码。这就是为什么我没有提到其他人的 Stack Overflow 线程。因此,除非线程有相同的问题,否则请不要引导我这样做。我正在使用 2 个函数,合并和合并排序。合并排序是递归的,我打算将列表分成两半,然后对每一半进行排序。然后,合并算法应该采用两个排序列表和 return 一个新列表,这只是两个列表的组合和排序。最终代码应该 return 一个完全排序的列表。下面是我的代码,如果你 运行 它你会看到我得到一个空列表 returned,这对我来说毫无意义。任何帮助将不胜感激。谢谢:)

def merge(left, right):
    resultList = []
    leastRight = 0
    leastLeft = 0
    if len(left) >= len(right):
        for i in range(len(left)-1):
            counter = 0
            for j in range(len(right)-1):
                counter += 1
                if right[counter % (len(right)-1)] <= right[j]:
                    leastRight = right[counter % (len(right)-1)]
                    print("leastRight if",leastRight)
                else:
                    leastRight = right[j]
                    print("leastRight else", leastRight)
                right.remove(leastRight)
            if left[i] <= leastRight:
                resultList.append(left[i])
            else:
                resultList.append(leastRight)
    else:
        for i in range(len(right)-1):
            counter = 0
            for j in range(len(left)-1):
                counter += 1
                if left[counter % (len(left)-1)] <= left[j]:
                    leastLeft = left[counter%(len(left)-1)]
                    print("leastLeft if", leastLeft)
                else:
                    leastLeft = left[j]
                    print("leastLeft else", leastLeft)
                left.remove(leastLeft)
            if right[i] <= leastLeft:
                resultList.append(right[i])
            else:
                resultList.append(leastLeft)
return (resultList)

def mergeSort(alist):

    print("alist", alist)
    if len(alist) > 2:
        middleIndex = len(alist) // 2
        sortedLeft = mergeSort(alist[:middleIndex])
        print("left", sortedLeft)
        sortedRight = mergeSort(alist[middleIndex:])
        print("right", sortedRight)
        result = merge(sortedLeft, sortedRight)
        print("result", result)
    else:
        result = alist
    return (result)

print("mergesort", mergeSort([6, 0, 2, 8, 9, 1]))

抱歉,您的合并功能方法根本无法使用。选择最小元素的原理在这里太纠结了,导致错误(我刚看到不能合并[6]和[0,2])。你读过关于合并的经典描述吗?

mergesort中(我们不能省略长度为2的列表的处理):

if len(alist)   >=    2:

合并例程的快速工作实现。

def merge(left, right):
    resultlist = []
    llen = len(left)
    rlen = len(right)
    il = 0
    ir = 0
    while il < llen and ir < rlen:  #while both list are not exhausted
        if left[il] <= right[ir]:    #choose the smallest current item
            resultlist.append(left[il])
            il += 1
        else:
            resultlist.append(right[ir])
            ir += 1
       #now treat the rest tail
    while il < llen:  
        resultlist.append(left[il])
        il += 1
    while ir < rlen:
        resultlist.append(right[ir])
        ir += 1
    return resultlist

结果

>>> mergesort [0, 1, 2, 6, 8, 9]

请注意,将 resultlist 的长度设为已知速度是明智的。

def merge(left, right):
    llen = len(left)
    rlen = len(right)
    resultlist = [0]*(llen+rlen) # we know data type and full length
    il = 0
    ir = 0
    k = 0
    while il < llen and ir < rlen:
        if left[il] <= right[ir]:
            resultlist[k] = left[il]
            il += 1
        else:
            resultlist[k] = right[ir]
            ir += 1
        k += 1
    while il < llen:
        resultlist[k] = left[il]
        il += 1
        k += 1
    while ir < rlen:
        resultlist[k] = right[ir]
        ir += 1
        k += 1
    return resultlist