C Program for merging two sorted array
#include<stdio.h>
#include<conio.h>
void main()
{
int a1[50],a2[50],a3[100],i,j,k,s1,s2,s3;
clrscr();
printf("Enter any two sorted array for merging them\n");
printf("Enter the Number of elements in first array\n");
scanf("%d",&s1);
printf("Now Enter %d elements\n",s1);
for(i=0;i<s1;i++)
scanf("%d",&a1[i]);
printf("\nNow Enter the Number of elements in second array\n");
scanf("%d",&s2);
printf("Now Enter %d elements\n",s2);
for(j=0;j<s2;j++)
scanf("%d",&a2[j]);
//Logic
i=0,j=0,k=0,s3=s1+s2;
while(i<s1 && j<s2)
{
if(a1[i]>a2[j])
a3[k++]=a2[j++];
else
a3[k++]=a1[i++];
}
if(i<s1)
while(i<s1)
a3[k++]=a1[i++];
else
while(j<s2)
a3[k++]=a2[j++];
printf("\nAfter Merging the elements in the array is\n");
for(k=0;k<s3;k++)
printf("%d ",a3[k]);
getch();
}
OUTPUT
------------------------------------------------------------------------------------------------------------
You can also visit:-