思路:
定义一个新的空列表
比较两个列表的首个元素
小的就插入到新列表里
把已经插入新列表的元素从旧列表删除
直到两个旧列表有一个为空
再把旧列表加到新列表后面
def loop_merge_sort(list1, list2):
tmp = []
while len(list1) > 0 and len(list2) > 0:
if list1[0] < list2[0]:
tmp.append(list1[0])
del list1[0]
elif list1[0] == list2[0]:
tmp.append(list1[0])
tmp.append(list2[0])
del list1[0]
del list2[0]
else:
tmp.append(list2[0])
del list2[0]
tmp.extend(list1)
tmp.extend(list2)
return tmp
a = [1, 2, 3, 7]
b = [3, 4, 5]
print(loop_merge_sort(a, b))
原文:https://www.cnblogs.com/laosun0204/p/14627374.html