the-honk/school/a-level/Y12 2022-2024/Algorithms/Merge Sort.py

20 lines
No EOL
460 B
Python

def mergeSort(lst: list) -> list:
if len(lst) > 1:
mid = len(lst) // 2
left = lst[:mid]
right = lst[mid:]
mergeSort(left)
mergeSort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
lst[k] = left[i]
i += 1
else:
lst[k] = right[j]
print(mergeSort([3,5,2,1]))