I am trying to implement the Merge Sort algorithm. I am following the algorithm mentioned in the CLRS book.Here is my code
#include
#include
The problem is very simple: your last loop
in the merge(...)
has a problem.
Move the l = 0
and m = 0
before the loop starts, because you are using the values 0 and 1 for l and m always, in each loop iteration.
Change it to:
int l=0, m=0;
for(i=0;i<=end_index;i++){
if(sub_arr1[l]<sub_arr2[m])
{arr[i]=sub_arr1[l++];}
else
{arr[i]=sub_arr2[m++];}
}