1/28/2024
Merge Sort
This is my attempt to solve the Sort an Array problem using merge sort.
Merge sort algorithm involves 2 functions: merge and sort.
The merge function is to merge 2 already sorted arrays into 1 sorted array. The sort function is to divide an array into 2 almost equal sub-arrays, recursively sort each array, and then merge two sub-arrays back using the merge function.
The terminate condition of the function sort
is when the lower bound is greater or equal to the higher bound, which is when the length of the sub-array is 0. When sort
function reaches the terminate condition, it will go back one step, now the sub-array has 1 element. What it does is just put that element back into the original array. Even though the function merge
named "merge", it is the one that does the sorting.