Hi, I wanted to ask this question in the course related topic but topic is dead with no replies for months. The following is the solution code for the merge sort algorithm where you split the list into individual components and merge them and I can’t understand how it works when the recursion comes in
def merge_sort(items): if len(items) <= 1: return items middle_index = len(items) // 2 left_split = items[:middle_index] right_split = items[middle_index:] left_sorted = merge_sort(left_split) right_sorted = merge_sort(right_split) return merge(left_sorted, right_sorted) def merge(left, right): result =  while (left and right): if left < right: result.append(left) left.pop(0) else: result.append(right) right.pop(0) if left: result += left if right: result += right return result
My understanding is
- The list gets inputted into the merge_sort() function
- List gets split into half.
- It goes into the merge function. The merge function takes the smaller first element from the two lists and appends it to result.
- If both left/right still has elements, add it to the end of the results list
- The process repeats through the recursion somehow? This is the part where I no longer understand the code
Could anyone shed light how it works after that? Thanks a bunch.