简体   繁体   English

两个数组的递归MergeSort

[英]Recursive MergeSort of Two Arrays

I am challenging myself to write code by hand for the purpose of interviews and I created a sub-par implementation of a recursive MergeSort. 我挑战自己要手工编写代码以进行访谈,并且我创建了递归MergeSort的低于标准实现。

The concept is basically taking two lists "alist" and "blist", and combining/sorting them into list "clist". 该概念基本上是采用两个列表“ alist”和“ blist”,并将它们组合/排序为列表“ clist”。 The lists are assumed sorted and the same size prior to combination. 假定列表在组合之前已排序并且大小相同。

How can I make the original code (which I know does not work properly) reflect the model code (working as intended) with the fewest amount of changes possible? 如何使原始代码(我知道不能正常工作)以最小的更改量反映模型代码(按预期工作)? Knowing the degree of my errors will help me greatly. 了解我的错误程度将对我有很大帮助。

Original Code: 原始代码:

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)

Model Code: 型号代码:

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))

There are two part to the algorithm 算法分为两部分

  • Merge Sort : Sort a list 合并排序:对列表进行排序
  • Merge : Merge two already sorted lists into a single sorted list 合并:将两个已经排序的列表合并到一个排序的列表中

Merge is missing in your code. 您的代码中缺少Merge

Algorithm: 算法:

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) is simple, since A and B are already sorted scan thought them picking the smallest element each time. 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]

Now finally the MergeSort 现在最后是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

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

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