You are on page 1of 2

visit-www.Codes2u.

com
#include<stdio.h>
#include<conio.h>
mergsort(int a[],int p,int r);
merge(int a[],int p,int q,int r);
void main()
{
int i,n,p,r;
int a[100];
printf("\n enter the no of elements to be sorted");
scanf("%d",&n);
printf("enter the array");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("End For Scan");
p=1;
r=n;
mergsort(a,p,r);
for(i=1;i<=n;i++)
{
printf("%d",a[i]); }
getch();
}
mergsort(int a[],int p,int r)
{int q;
if(p<r)
{
q=(p+r)/2;
mergsort(a,p,q);
mergsort(a,q+1,r);
merg(a,p,q,r);
}
}
merg(int a[],int p,int q,int r)
{
int L[100],R[100],Ln,Rn,i,j,k;
Ln=q-p+1;
Rn=r-q;
for(i=1;i<=Ln;i++)
L[i]=a[p+i-1];
for(i=1;i<=Rn;i++)
R[i]=a[q+i];
L[Ln+1]=1000;

R[Rn+1]=1000;
i=1;
j=1;
for(k=p;k<=r;k++)
{
if(L[i]<=R[j])
{
a[k]=L[i];i++;
}
else
{
a[k]=R[j];j++;
}
}
}

You might also like