Sunday, October 2, 2011

Merge Sort C program for Dev c++

 merges(int a[],int l,int r)  
{
int c=(l+r)/2,i=l;
int j=c+1,k=0,x;
int b[r-l+1];
while(j<=r && i<=c)
{
if(a[j]<a[i])
b[k++]=a[j++];
else
b[k++]=a[i++];
}
while(j<=r)
b[k++]=a[j++];
while(i<=r)
b[k++]=a[i++];
for(x=l;x<=r;x++)
a[x]=b[x-l];
}
merge_rec(int a[],int l,int r)
{
int c;
if(l<r)
{
c=(l+r)/2;
merge_rec(a,l,c);
merge_rec(a,c+1,r);
merges(a,l,r);
}
}
int main()
{
int a[]={1,4,2,11,0,45,9,75,-4,40};
int i;
merge_rec(a,0,9);
for(i=0;i<=9;i++)
{
printf("%d ",a[i]);
}
getch();
}

No comments:

Post a Comment

Google+ Followers