- 时间复杂度O(nlogN)
- 不稳定排序
def merge(arrA, arrB):
arr = []
i,j = 0,0
while i < len(arrA) and j < len(arrB):
if arrA[i] < arrB[j]:
arr.append(arrA[i])
i+=1
else:
arr.append(arrB[j])
j+=1
for start in range(i, len(arrA)):
arr.append(arrA[start])
for start in range(j, len(arrB)):
arr.append(arrB[start])
return arr
def merge_sort(arr):
if len(arr) <= 1:
return
mid = len(arr) // 2
arrA = merge_sort(arr[:mid])
arrB = merge_sort(arr[mid:])
return merge(arrA, arrB)