def merge_sort_list(sort_a, sort_b):
length_a, length_b =len(sort_a), len(sort_b)
a = b = 0
new_sorted_seq = list()
# 实际在len=1 才比较
while a < length_a and b < length_b:
if sort_a[a] < sort_b[b]:
new_sorted_seq.append(sort_a[a])
a += 1
else:
new_sorted_seq.append(sort_b[b])
b += 1
if a < length_a:
new_sorted_seq.extend(sort_a[a:])
else:
new_sorted_seq.extend(sort_b[b:])
return new_sorted_seq
def merge_sort(seq):
if len(seq) <= 1:
return seq
else:
mid = int(len(seq)/2)
left_half = merge_sort(seq[:mid])
right_half = merge_sort(seq[mid:])
new_seq = merge_sort_list(left_half, right_half)
return new_seq
if __name__ == '__main__':
seq = [1, 49, 9, 10, 23, 4, 2, 5]
result = merge_sort(seq)
print(result)
网友评论