Implement the Merge Sort Algorithm - Step 20

Tell us what’s happening:

I have a question about why we need to first while loop that checks if left_array_index < len(left_part) and right_array_index < len(right_app) if the last two while loops check that already?

Your code so far

def merge_sort(array):
# User Editable Region
    
# User Editable Region
    
    middle_point = len(array) // 2
    left_part = array[:middle_point]
    right_part = array[middle_point:]
    
    merge_sort(left_part)
    merge_sort(right_part)
    
    left_array_index = 0
    right_array_index = 0
    sorted_index = 0
    
    while left_array_index < len(left_part) and right_array_index < len(right_part):
        if left_part[left_array_index] < right_part[right_array_index]:
            array[sorted_index] = left_part[left_array_index]
            left_array_index += 1
        else:
            array[sorted_index] = right_part[right_array_index]
            right_array_index += 1
        sorted_index += 1
    
    while left_array_index < len(left_part):
        array[sorted_index] = left_part[left_array_index]
        left_array_index += 1
        sorted_index += 1
    
    while right_array_index < len(right_part):
        array[sorted_index] = right_part[right_array_index]
        right_array_index += 1
        sorted_index += 1

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36

Challenge Information:

Implement the Merge Sort Algorithm - Step 20

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-merge-sort/6569b743630ee592a65a7e2f.md at main · freeCodeCamp/freeCodeCamp · GitHub

First while loop is for case when both merging lists still have elements - they need to be compared to sort them.
After that remains case(s) when only one list still have elements, but the other one is now empty.