Hello guys,I have this Merge Sort Program. I am not able to understand and when I tried to understand with debugging ,it does not give output too. Can someone help me out with explanation of this recursive msort and merge function? I am really sorry to bother but I want to learn this sorting technique. I understand manual but I don't understand through this code.
Code:
main()
{
int i,low,high,n,a[10];
printf("how many elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i);
low=0;
high=n-1;
msort(a,low,high);
printf("The sorted array is ");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
msort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
msort(a,low,mid);
msort(a,mid+1,high);
merge(a,low,mid,high);
}
}
merge(int a[],int low ,int mid ,int high)
{
int h,k,i,j,b[10];
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i=i+1;
}
}
else
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i=i+1;
}
for(k=low;k<=high;k++)
{
a[k]=b[k];
}
}