I am trying to understand this code that I have found online however, I can't understand it because something is confusing me. As you can see below, I have put a asterisk on the confusing part. At *1 it is running the 'merge_sort' function recursively so how come at *2 it is able to return the function 'merge'.I would
ofhave thought that the compiler couldn't run *2 because of the recursive call at list 1*.
please could someone explain to me how this works because I cannot progress further until I can find out how it works.
What I have tried:
def merge_sort(array):
if len(array) <= 1:
return array
midpoint = int(len(array) / 2)
*1 left, right = merge_sort(array[:midpoint]), merge_sort(array[midpoint:])
*2 return merge(left, right)
def merge(left, right):
result = []
left_pointer = right_pointer = 0
while left_pointer < len(left) and right_pointer < len(right):
if left[left_pointer] < right[right_pointer]:
result.append(left[left_pointer])
left_pointer += 1
else:
result.append(right[right_pointer])
right_pointer += 1
result.extend(left[left_pointer:])
result.extend(right[right_pointer:])
return result