def merge_sort(arr):
length = len(arr)
if length <= 1:
return arr
mid = length // 2
left = merge_sort(arr[mid:])
right = merge_sort(arr[:mid])
return merge(left, right)
def merge(arr1, arr2):
l, r = 0, 0
res = []
while l < len(arr1) and r < len(arr2):
if arr1[l] > arr2[r]:
res.append(arr2[r])
r += 1
else:
res.append(arr1[l])
l += 1
res += arr1[l:]
res += arr2[r:]
return res
if __name__ == '__main__':
l = [5, 9, 1, 11, 6, 7, 2, 4]
print(merge_sort(l))