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?
Bu egzersiz
Data Structures and Algorithms in Python
kursunun bir parçasıdırEgzersiz talimatları
- 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.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
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)