Run Time Error During Merge Sort
i keep getting a runtime error every time i run this code, the algorithm seems to be right, i am using long values instead of integers because the array's size is large.
so what seems to be the problem?
void mergeSort(long left,long right, int a[],long n)
{
cloc开发者_Go百科k_t ts,te;
ts=clock();
m2(left,right,a,n);
te=clock();
times[4]+=((double)(te-ts)/CLOCKS_PER_SEC)*1000;
}
void m2(long left,long right, int a[],long n)//related to mergesort
{
int center;
if( left < right )
{
center = (left + right) / 2;
mergeSort(left,center,a,n);
mergeSort(center+1,right,a,n);
merge(left,right,center,a,n);
}
}
Thanks in Advance
Nathalie;
Center is int
, should be at least long
.
In m2
, you should recurse into m2
and not mergeSort
because you don't want to time the sub-calculations.
Possible cause for runtime error: stack overflow (pun :D) you are doing recursion and using O(n) space on the stack. Can't say anything else if you don't give us more information.
精彩评论