#时间复杂度O(nlogn),空间复杂度O(n)
def merge(left,right):
#
i,j =0,0
result =[]
while i<len(left) and j<len(right):
if left[i]<=right[j]:
result.append(left[i])
i +=1
else:
result.append(right[j])
j +=1
result+=left[i:]#i,j到了序列的末尾
result+=right[j:]
return result
def merge_sort(lists):
#归并排序
if len(lists)<=1:#将序列不断分为小切片
return lists
mid =len(lists)//2
left =merge_sort(lists[:mid])
right =merge_sort(lists[mid:])
return merge(left,right)
lists =[2,-2,66,2,1,6,5]
for i in merge_sort(lists):
print(i,end =" ")