Merge Sort Error C

后端 未结 1 1509
一整个雨季
一整个雨季 2021-01-28 21:20

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

        
相关标签:
1条回答
  • 2021-01-28 22:10

    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++];}
    
    }
    
    0 讨论(0)
提交回复
热议问题