# Implement the Merge Sort Algorithm - Step 20

**URL:** <https://forum.freecodecamp.org/t/implement-the-merge-sort-algorithm-step-20/802671>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [October 23, 2026, 11:21pm UTC](https://forum.freecodecamp.org/t/implement-the-merge-sort-algorithm-step-20/802671 "2026-10-23T23:21:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashley.varghese11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ashley.varghese11/32/420476_2.png) [@ashley.varghese11](https://forum.freecodecamp.org/u/ashley.varghese11)\
**Post date:** [October 23, 2026, 11:21pm UTC](https://forum.freecodecamp.org/t/implement-the-merge-sort-algorithm-step-20/802671/1 "2026-10-23T23:21:02Z")

</div>

### 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

```py
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

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/python-v9/workshop-merge-sort/step-20)**
>
> Learn to Code for Free

GitHub Link: [freeCodeCamp/curriculum/challenges/english/blocks/workshop-merge-sort/6569b743630ee592a65a7e2f.md at main · freeCodeCamp/freeCodeCamp · GitHub](https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/workshop-merge-sort/6569b743630ee592a65a7e2f.md)

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [October 24, 2026, 4:02am UTC](https://forum.freecodecamp.org/t/implement-the-merge-sort-algorithm-step-20/802671/2 "2026-10-24T04:02:16Z")

</div>

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.
