import random
A = []
for i in range(random.randint(0, 100)):
A.append(random.randint(0, 100))
print A
def merge_sort(A):
if len(A) <= 1:
return A
def merge(A1, A2):
merged = []
while A1 and A2:
if A1[0] <= A2[0]:
merged.append(A1.pop(0))
else:
merged.append(A2.pop(0))
while A1:
merged.append(A1.pop(0))
while A2:
merged.append(A2.pop(0))
return merged
m = len(A)/2
A1 = merge_sort(A[:m])
A2 = merge_sort(A[m:])
return merge(A1, A2)
print merge_sort(A)
【python菜鸟日记】-03算法导论-归并排序
最新推荐文章于 2021-10-08 14:40:45 发布