我在python中的合并排序有什么问题?

Joh*_*ohn -1 python mergesort

我想写合并排序并卡在这里.

我的代码有什么问题?我试图在不引用任何资源的情况下实现它,并且不必要地编写这一行,因为Stackoverflow中的一些愚蠢的规则迫使我解释我的代码.

def merge_sort(A):
    if len(A) <= 1:
        return A

    #split list in 2
    mid = len(A)/2
    B = A[:mid]
    C = A[mid:]

    B = merge_sort(B)
    C = merge_sort(C)

    #merge
    result = []
    while len(B) > 0 and len(C) > 0:
        if B[0] > C[0]:
            result.append(C.pop(0))
        else:
            result.append(B.pop(0))

    if len(B) > 0:
        result.extend(merge_sort(B))
    else:
        result.extend(merge_sort(C))



print merge_sort([8, 2, 1, 1, 4, 45, 9, 3])
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Traceback (most recent call last):
  File "merge_sort.py", line 31, in <module>
    print merge_sort([8, 2, 1, 1, 4, 45, 9, 3])
  File "merge_sort.py", line 11, in merge_sort
    B = merge_sort(B)
  File "merge_sort.py", line 16, in merge_sort
    while len(B) > 0 and len(C) > 0:
TypeError: object of type 'NoneType' has no len()
Run Code Online (Sandbox Code Playgroud)

gae*_*123 8

你需要merge_sort()函数

return result 
Run Code Online (Sandbox Code Playgroud)

在最后但它没有.函数默认返回None,这就是您收到错误的原因.