My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

merge sort

Iam programmer's photo
Iam programmer
·Nov 8, 2017

this is a function that split the array into subsets recursively ..

My problem is that I don't understand how the function will recurse twice..Isn't is supposed to recurse first with new arguments(a,i,mid).. how will it then return and take new arguments(a,mid+1,j) if it already broke to recurse at first place?

void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion//my problem//
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}