#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],b[20],c[40];
int p,q,r;
clrscr();
printf("\nEnter p: ");
scanf("%d",&p);
enter(a,p);
printf("\nEnter q: ");
scanf("%d",&q);
enter(b,q);
r=merge(a,b,c,p,q);
printf("\nFirst array is \n");
display(a,p);
printf("\nSecond array is \n");
display(b,q);
printf("\nThird array is \n");
display(c,r);
getch();
return 0;
}
int enter(int a[],int z)
{
int i;
printf("\nEnter %d sorted values \n",z);
for(i=0;i < z;i++)
{
scanf("%d",&a[i]);
}
return;
}
int display(int a[],int z)
{
int i;
for(i=0;i < z;i++)
{
printf("%4d",a[i]);
}
return;
}
int merge(a,b,c,p,q)
int a[],b[],c[];
int p,q;
{
int i,j,k;
i=j=k=0;
while(i < p && j < q)
{
if (a[i] < b [j])
{
c[k++]=a[i++];
}
else
{
c[k++]=b[j++];
}
}
while (i < p)
{
c[k++]=a[i++];
}
while(j < q)
{
c[k++]=b[j++];
}
return(k);
}
Friday, 27 April 2012
Program for Merge Sort in C
Labels:
Algorithms with Average Case Complexity = O(n log n),
Basic Programs in C,
C,
C Programs for B.Tech CSE,
C Programs for Engineers,
Data Structures and Algorithms,
Data Structures and Algorithms in C,
Implementation of Sorting Algorithms in C,
Implementations in C,
Important C Programs for Interviews,
Important Programs in C++,
Interesting Programs in C Language,
O(n log n),
Programming for Engineers,
Sorting,
Very Important Programs in C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment