LEARNING JOURNAL

I created this learning journal to practice writting and to help me learn by writting everything down. Source code here

2/5/2024

Sort an Array - Merge Sort Bottom Up approach 2nd attempt

I just figured why we need to set the condition for ending the inner for loop to be lo <= nums.length - size.

It is because mid = lo + size - 1 and we need there is always at least 1 array to merge. If there is one array to merge, the last clause of the if-else series in the merge function will be executed to copy the element from aux back to the original array.

If we want there is always at least 1 arrary to merge, the mid point can not pass the last element, which is nums.length - 1, so, mid <= nums.length -1, thus lo + size - 1 <= nums.length -1. So, lo <= nums.length - size.