Complete the code to split the list into two halves.
mid = len(arr) [1] 2
We use integer division (//) to find the middle index so that it is an integer.
Complete the code to recursively sort the left half of the list.
left_sorted = merge_sort(arr[1]:mid)We use square brackets [] to slice the list from start to mid.
Fix the error in the merge step to append the smaller element.
if left[i] [1] right[j]: merged.append(left[i]) i += 1
We append the smaller element, so we check if left[i] is less than right[j].
Fill both blanks to complete the loops that add remaining elements after merging.
while i [1] len(left): merged.append(left[i]) i += 1 while j [2] len(right): merged.append(right[j]) j += 1
We continue while i and j are less than the lengths of their lists to add remaining elements.
Fill all three blanks to complete the merge_sort function header, base case, and return statement.
def merge_sort(arr): if len(arr) [1] 1: return arr mid = len(arr) // 2 left = merge_sort(arr[:mid]) right = merge_sort(arr[mid:]) merged = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: merged.append(left[i]) i += 1 else: merged.append(right[j]) j += 1 while i < len(left): merged.append(left[i]) i += 1 while j < len(right): merged.append(right[j]) j += 1 return [2]
The base case checks if length is less than or equal to 1, then returns the merged sorted list.
