展开全部
因为merge_sort函数没有返回值2113,所以l1=merge_sort(left)和r1=merge_sort(right)中出l1和r1没有类型的错误,加一5261个返回值return li就没问题了.
完整4102的Python程序如下1653(改动的地方见注释)def merge_sort(li):
if len(li)==1:
return li #这里return改成return li
mid=len(li)//2
left=li[:mid]
right=li[mid:]
l1=merge_sort(left)
r1=merge_sort(right)
return merge(l1,r1)
def merge(left,right):
result=[]
while len(left)>0 and len(right)>0:
if left[0]<=right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
result+=left
result+=right
return result
a=[2,39,92,19,28,32,85,53]
print(merge_sort(a))
源代码(注意源代码的缩进)