示例
def merge(left, right):
merge_list = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
merge_list.append(left[i])
i += 1
else:
merge_list.append(right[j])
j += 1
if i == len(left):
for i in right[j:]:
merge_list.append(i)
else:
for j in left[i:]:
merge_list.append(j)
return merge_list
def merge_sort(lists):
if len(lists) <= 1:
return lists
middle = len(lists) // 2
left = merge_sort(lists[:middle])
right = merge_sort(lists[middle:])
return merge(left, right)
print(merge_sort([187, 87, 43, 67, 12, 200, 34, 67]))