标签:index python 合并排序 pytho 结果 ini end bsp odi
1 #!/usr/bin/python 2 # --coding:utf-8 -- 3 4 def sort_merge(left,right): 5 i,j=0,0 6 result=[] 7 while i<len(left) and j<len(right): 8 if left[i]<right[j]: 9 result.append(left[i]) 10 i+=1 11 else: 12 result.append(right[j]) 13 j+=1 14 result+=left[i:] 15 result+=right[j:] 16 return result 17 18 19 def sortdata(input): 20 if len(input)==1: 21 return input 22 index=len(input)//2 23 left=sortdata(input[:index]) 24 right=sortdata(input[index:]) 25 return sort_merge(left,right) 26 27 print(sortdata([3,5,9,10,13,35,56])) 28 29 30 31 # left right 32 # 3,5,9 10,3,35,56 33 # left right left right 34 # 3 5,9 10 ,3 35,56 35 # left right left right left right 36 # 5 9 10 3 35 56
结果:
[3, 5, 9, 10, 13, 35, 56]
[Finished in 0.5s]
标签:index python 合并排序 pytho 结果 ini end bsp odi
原文地址:http://www.cnblogs.com/sayhihi/p/7649712.html