I wrote a recursive merge sort program , but it seems to get stuck whilst reading the array input itself(loop never terminates), I am not a pro when it comes to C ,I know it is some simple embarrassing mistake ,after hours no still no good , Please help:
//merge sort
#include<stdio.h>
#include<stdlib.h>
int a[20];
void mergeSort(int,int);
void merge(int,int,int);
void main()
{
//int a[20];
int n=0;
int i=0;
int x=0;
printf("Enter n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Loop is over");
int low=0;
int high=n;
//start the sorting
mergeSort(low,high);
//done, print the result
//
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
//return 0;
}//main ends
void mergeSort(int low,int high)
{
while(low<high)
{
//split
int mid=(low+high)/2;
mergeSort(low,mid);
mergeSort(mid+1,high);
merge(low,mid,high);
}
}//mergeSort ends
void merge(int low,int mid,int high)
{
//two lists , sort them
int b[50];
int i,k,h,j;//a[],b[],h->low,j->mid+1
i=0;k=0;h=low;j=mid+1;
while(h<=mid && j<high)
{
if(a[h]<a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)//left has emptied, copy whats left in right
{
for(k=j;k<high;k++)
{
b[i]=a[k];
i++;
}
}
else//right has emptied
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
//copy b to a
for(k=0;k<high;k++)
{
a[k]=b[k];
}
}//merge ends
Add the below checks and you should be able to figure out