Friday, 22 June 2012

write a c program to sort an array element using merge sort techniques


/*write a c program to sort an array element using merge sort techniques*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],c[40],n,m,i,j,k,temp=0;
clrscr();
printf("enter size for first array:-");
scanf("%d",&m);
printf("enter size for second array:-");
scanf("%d",&n);
printf("\nenter number for an first array :-");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("\nenter number for an second array :-");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<m;i++)
{
for(j=i+1;j<m;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
printf("\nthe sorted array is:-\n");
for(i=j=k=0;i<m+n;)
{
if(a[j]<=b[k])
c[i++]=a[j++];
else
c[i++]=b[k++];
if(j==m||k==n)
break;
}
while(j<m)
c[i++]=a[j++];
while(k<n)
c[i++]=b[k++];
printf("\nthe array after sorting\n");
for(i=0;i<m+n;i++)
printf("%d\n",c[i]);
getch();
}
/*enter size for first array:-4
enter size for second array:-2

enter number for an first array :-3
6
5
4

enter number for an second array :-2
5

the sorted array is:-

the array after sorting
2
3
4
5
5
6 */

No comments: