merge sort not working properly,it does not give a sorted array
i couldnt find error in the problem but when it is run i lost some numbers
,rest all gets sorted up,run this program and you will get to know what i
wanna say
#include #include #define size 20
void partition(int a[],int low,int upr)
{
int mid;
if(low<upr)
{
mid=(low+upr)/2;
partition(a,low,mid);
partition(a,mid+1,upr);
merge(a,low,mid,upr);
}
}
void merge(int a[],int low,int mid,int upr)
{
int c[size];
int i=low;
int j=mid+1;
int k=low;
while(i<mid+1&&j<upr+1)
{
if(a[i]<a[j])
{
c[k]=a[i];
i++;
k++;
}
else
{
c[k]=a[j];
j++;
k++;
}
}
if(i<mid+1)
{
c[k]=a[i];
i++;
k++;
}
if(j<upr+1)
{
c[k]=a[j];
j++;
k++;
}
for(k=low; k<=upr; k++)
a[k]=c[k];
}
int main()
{
int n,i,a[size];
printf("enter the total no of elements less than %d",size);
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
partition(a,0,n-1);
printf("\nsorted array \n");
for(i=0;i<n;i++)
printf("%d \n",a[i]);
return 0;
}
pls help me out
No comments:
Post a Comment