Theory
Merge Sort divides the array into halves until each subarray has one element, then merges them back together in sorted order.
Implementation
def merge_sort(array):
if len(array) == 1:
return array
else:
mid = len(array)//2
left_array = array[:mid]
right_array = array[mid:]
print(f'Left : {left_array}')
print(f'Right : {right_array}')
return merge(merge_sort(left_array),merge_sort(right_array))
def merge(left, right):
l = len(left)
r = len(right)
left_index = 0
right_index = 0
sorted_array = []
while (left_index < l and right_index < r):
global count
count += 1
if left[left_index] < right[right_index]:
sorted_array.append(left[left_index])
left_index += 1
else:
sorted_array.append(right[right_index])
right_index += 1
print(sorted_array + left[left_index:] + right[right_index:])
return sorted_array + left[left_index:] + right[right_index:]Runtime
- Time:
- Space: