Get startedGet started for free

Correcting a bug in the merge sort algorithm

You have been given a program that sorts a list of numbers using the merge sort algorithm. While testing the merge_sort() function, you realize that the code is not correct. Can you correct the algorithm so that it works correctly?

This exercise is part of the course

Data Structures and Algorithms in Python

View Course

Exercise instructions

  • Correct the mistake when assigning the left half.
  • Correct the mistake when assigning the right half.
  • Correct the mistake when updating the pointer for the left half.
  • Correct the mistake when updating the pointer for the right half.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def merge_sort(my_list):
    if len(my_list) > 1: 
        mid = len(my_list)//2
        left_half = my_list[:mid]
        right_half = my_list[mid:]
        
        merge_sort(left_half)
        merge_sort(right_half)
 
        i = j = k = 0
 
        while i < len(left_half) and j < len(right_half):
            if left_half[i] < right_half[j]:
        		# Correct mistake when assigning left half
                my_list[k] = right_half[i]                
                i += 1
            else:
                # Correct mistake when assigning right half
                my_list[k] = left_half[j]
                j += 1
            k += 1
            
        while i < len(left_half):
            my_list[k] = left_half[i]
            # Correct mistake when updating pointer for left half
            j += 1
            k += 1
 
        while j < len(right_half):
            my_list[k] = right_half[j]
            # Correct mistake when updating pointer for right half
            i += 1
            k += 1

my_list = [35,22,90,4,50,20,30,40,1]
merge_sort(my_list)
print(my_list)
Edit and Run Code