#-*-coding:UTF-8-*-
def merge(arr1, arr2):
arr = []
len1 = len(arr1)
len2 = len(arr2)
i=0
j=0
while i<len1 and j<len2:
if arr1[i]<arr2[j]:
arr.append(arr1[i])
i+=1
else:
arr.append(arr2[j])
j+=1
for m in range(i,len1):
arr.append(arr1[m])
for n in range(j,len2):
arr.append(arr2[n])
return arr
def merge_sort(arr,p,q):
h=int((p+q)/2)
if p>=q:
return
merge_sort(arr,p,h)
merge_sort(arr,h+1,q)
return merge(arr[p:h],arr[h+1:q])
print merge_sort([1,2,4,5,7,2,4,7,9,10],0,9)
算法导论合并排序算法python实现
最新推荐文章于 2022-10-12 14:13:09 发布