MERGING AND SORTING
MERGING AND SORTING:-
CODE:-
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, n1, n2, k, a1[20], a2[20], m[20];
clrscr();
printf("enter size of first array : ");
scanf("%d", &n1);
printf("\n");
for (i=0; i<n1; i++)
{
printf("enter array element :");
scanf( "%d", &a1[i] );
}
printf("\n");
printf("enter size of second array :");
scanf( "%d", &n2 );
printf("\n");
for(j=0; j<n2; j++)
{
printf("enter array element :");
scanf( "%d", &a2[j] );
}
printf("\n\n The first array :");
for(i=0; i<n1; i++)
{
printf("%d ", &a1[i]);
}
printf("\n\n The Second array :");
for(j=0; j<n2; j++)
{
printf("%d ", &a2[j]);
}
k=0;
i=0; j=0;
while(i<n1 && j<n2)
{
if(a1[i]<a2[j])
{
m[k++] = a1[i++];
}
else
{
m[k++] = a2[j++];
}
}
while(i<n1)
m[k++] = a1[i++];
while(j<n2)
m[k++] = a2[j++];
printf("\n\n Array after merging and sorting :");
for(i=0; i<n1+n2; i++)
{
printf("%d ",m[i]);
}
getch();
return 0;
}
Comments
Post a Comment
Kaushikmadhav77@gmail.com