1 def merge(ll, rr): 2 i = 0 3 j = 0 4 num = [] 5 while i < len(ll) and j < len(rr): 6 if ll[i] < rr[j]: 7 num.append(ll[i]) 8 i += 1 9 else: 10 num.append(rr[j]) 11 j += 1 12 if i != len(ll): 13 num += ll[i:] 14 else: 15 num += rr[j:] 16 return num 17 def mergesort(num): 18 if len(num) == 1: 19 return num 20 ll = mergesort(num[len(num)>>1:]) 21 rr = mergesort(num[:len(num)>>1]) 22 num = merge(ll, rr) 23 return num 24 25 print(mergesort([2,3,5,7,3,213,123,123,132,31,12,551,52,5,125,15,2]))
python的语法就是如此简洁
时间: 2024-10-08 18:52:07