def top_down(arr):
if len(arr) == 1:
return arr
arr1, arr2 = split_(arr)
res1 = top_down(arr1)
res2 = top_down(arr2)
res = sort_(res1,res2)
return res
def split_(arr):
"""
:return: arr_1,arr_2(切分后的结果)
"""
arr_1 = arr[0:len(arr)//2]
arr_2 = arr[len(arr)//2:]
return arr_1,arr_2
def sort_(a,b):
res = []
a_index = 0
b_index = 0
while a_index != len(a) and b_index != len(b):
if a[a_index]>b[b_index]:
res.append(b[b_index])
b_index += 1
else:
res.append(a[a_index])
a_index += 1
if a_index!=len(a):
res.extend(a[a_index:])
else:
res.extend(b[b_index:])
return res
if __name__ == '__main__':
arr = [6,3,-2,0,-1,5,9,10,9]
res = top_down(arr)
print(res)
<<<
[-2, -1, 0, 3, 5, 6, 9, 9, 10]