The basic idea to to take the array, divide it in half,
sort each half, and then merge the two halves together. It
turns out that merging two already sorted list can be done in linear
time:
def merge(left, right):
merged = []
left_index = 0
right_index = 0
# Merge the two halves in sorted order
while left_index < len(left) and right_index < len(right):
if left[left_index] <= right[right_index]:
merged.append(left[left_index])
left_index += 1
else:
merged.append(right[right_index])
right_index += 1
# Append any remaining elements
merged.extend(left[left_index:])
merged.extend(right[right_index:])
return merged