Write a Program in C to Accept 5 Numbers from the User and Sort Using Merge Sort
#include <stdio.h>#include <conio.h>
void main( )
{
int num1[3]={2,9,11};
int num2[3]={1,3,17};
int ans[6] ;
int i,j,k,l,temp ;
clrscr( ) ;
j=0,k=0;
printf("Orignal Numbers\n");
printf("num1[]=");
for(i=0;i<=2;i++)
{
printf("%d ",num1[i]);
}
printf("\nnum2[]=");
for(i=0;i<=2;i++)
{
printf("%d ",num2[i]);
}
printf("\n\n\n");
for(i=1;i<=6;i++)
{
printf("Comparing num1[%d] and num2[%d]\t: ",j+1,k+1);
if(num1[j]<=num2[k])
{
ans[i]=num1[j] ;
j++;
}
else
{
ans[i]=num2[k] ;
k++;
}
for(l=1;l<=i;l++)
{
printf("%d\t",ans[l]);
}
printf("\n\n");
if(j==3||k==3)
break;
}
i++;
if(j==2)
{
ans[i]=num1[j];
}
if(k==2)
{
ans[i]=num2[k];
}
printf("%d\n\n",ans[i]);
printf("Numbers after using Merge Sort Method\n");
for(i=1;i<=6;i++)
{
printf("%d\n",ans[i]);
}
getch( ) ;
}
No comments:
Post a Comment