1st mergesort function is called till the if condition is satisfied Now according to my knowledge if if-conditon is not satisfied then its body will not execute
I m unable to understand after the unsatisfaction of if-conditon 2nd mergesort function is called.so how it happens? Plz explain in detail
mergesort (int*a,int*b,int low,int high)
{
int pivot
if(low<high)
{
pivot=(low+high)/2;
mergesort(a,b,low,pivot);/*1st*/
mergesort(a,b,pivot+1,high);/*2nd*/
merge(a,b,low,pivot,high);
}
return ;
}
To follow up on Rohlex32's answer (his answer should get credit for the correct answer).
my question is why mergesort(a,b,pivot+1,high) is called?
As answered by Rohlex32, it's because mergesort(a, b, low, high), uses high as the last index to be sorted, so mergesort(a, b, low, pivot), includes the element at [pivot].
An alternative implementation of mergesort uses a beginning and ending index, where the ending index is 1 greater than the last index. The initial call to mergesort would be
mergesort(a, b, 0, sizeof(a)/sizeof(a[0]));
Such a mergesort function would be a bit different than above:
void mergesort(int *a, int *b, int begin, int end)
{
int mid = (begin+end)/2;
if((end - begin) < 2)
return;
mergesort(a, b, begin, mid); // sort from begin to mid-1
mergesort(a, b, mid, end); // sort from mid to end-1
merge(a, b, begin, mid, end); // merge the two parts
}