【问题标题】:Recursive MergeSort of Two Arrays两个数组的递归合并排序
【发布时间】:2019-09-09 04:48:09
【问题描述】:

我正在挑战自己为了面试而手工编写代码,并且我创建了一个递归 MergeSort 的低于标准的实现。

这个概念基本上是采用两个列表“alist”和“blist”,并将它们组合/排序到列表“clist”中。假设列表在组合之前已排序且大小相同。

如何使原始代码(我知道它不能正常工作)反映模型代码(按预期工作)以尽可能少的更改?了解我的错误程度会对我有很大帮助。

原始代码:

alist = [1,5,8,9]
blist = [2,4,7,10]
clist = []
temp = []

def MergeSort(alist,blist):
    if len(alist) > 1:
        midpoint = len(alist)//2
        MergeSort(alist[midpoint:],alist[:midpoint])
    if len(blist) > 1:
        midpoint = len(blist)//2
        MergeSort(blist[midpoint:],blist[:midpoint])
    if alist[0] < blist[0]:
        temp[0] = alist[0]
        alist[0] = blist[0]
        blist[0] = temp[0]
        MergeSort(alist,blist)
    else:
        alist[len(alist)] = blist[len(blist)-1]
        MergeSort(alist,blist)
    if blist[0] == None:
        return alist

clist = MergeSort(alist,blist)
print(clist)

型号代码:

alist = [1,5,8,9]
blist = [2,4,7,10]

def MergeSort(alist, blist):
    clist = []
    if alist == [] and blist != []:
        return clist + blist

    if alist != [] and blist != []:
        if alist[0] <= blist[0]:
            clist.append(alist[0])
            clist = clist + MergeSort(alist[1:], blist)
        if alist[0] > blist[0]:
            clist.append(blist[0])
            clist = clist + MergeSort(alist, blist[1:])
    return clist

print(MergeSort(alist,blist))

【问题讨论】:

    标签: python list sorting recursion


    【解决方案1】:

    算法分为两部分

    • 合并排序:对列表进行排序
    • 合并:将两个已排序的列表合并到一个排序列表中

    您的代码中缺少Merge

    算法:

    MergeSort(A, B)
         1.1 MergeSort(first_half_of_A, second_half_A)
         // Now first_half_of_A and second_half_A are in sorted order independently   
         1.2 Merge(first_half_of_A, first_half_of_A)
         // Now A is fully sorted
    
         2.1 MergeSort(first_half_of_B, second_half_B)
         2.2 Merge(first_half_of_B, second_half_B)
         // Now B is fully sorted
    
         3. Merge(A, B)
    

    Merge(A,B) 很简单,因为 AB 已经排序扫描,认为它们每次都选择最小的元素。

    def merge(alist,blist): 
        temp = list()
        i = 0
        j = 0
    
        while i < len(alist) and j < len(blist) :
            if  alist[i] < blist[j] :
                temp.append(alist[i])
                i += 1
            else:
                temp.append(blist[j])
                j += 1
    
        while i < len(alist): 
            temp.append(alist[i])
            i += 1
    
        while j < len(blist): 
            temp.append(blist[j])
            j += 1
    
        return temp
    
    # Test case
    assert merge([1,5,8,9], [1,2,3,4]) == [1, 1, 2, 3, 4, 5, 8, 9]
    

    现在终于MergeSort

    def MergeSort(alist,blist):
        if len(alist) > 1:
            midpoint = len(alist)//2
            MergeSort(alist[midpoint:],alist[:midpoint])
            alist[:] = merge(alist[midpoint:], alist[:midpoint])
        if len(blist) > 1:
            midpoint = len(blist)//2
            MergeSort(blist[midpoint:],blist[:midpoint])
            blist[:] = merge(blist[midpoint:], blist[:midpoint])
    
        return merge(alist, blist)
    
    # Test Case
    assert MergeSort([1,5,8,9], [2,4,7,10]) == [1, 2, 4, 5, 7, 8, 9, 10]
    
    # Testing
    import numpy as np
    for i in range(1000):
        a = np.random.rand(100)
        b = np.random.rand(100)
        c = np.append(a,b)
        assert np.sum(MergeSort(a,b)-np.sort(c)) == 0
    

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2019-01-22
      相关资源
      最近更新 更多