#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],b[50],i,j,k,n1,n2,c[100];
printf("Enter the lenght of fist array\n");
scanf("%d",&n1);
printf("Enter the lenght of second array\n");
scanf("%d",&n2);
printf("Enter first sorted array\n");
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter second sorted array\n");
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
// logic for merging the arrays
i=j=k=0;
while(i<n1 && j<n2)
{
if(a[i]>b[j])
{
c[k]=b[j];
j++;
}
else
{
if(a[i]==b[j])
{
c[k]=b[j];
j++;
i++;
}
else
{
c[k]=a[i];
i++;
}
}
k++;
}
while(i<n1)
{
c[k]=a[i];
k++;
i++;
}
while(j<n2)
{
c[k]=b[j];
j++;
k++;
}
printf("The merge array is: ");
for(i=0;i<k;i++)
printf("%d ",c[i]);
getch();
return 0;
}
Enter the lenght of fist array
5
Enter the lenght of second array
5
Enter first sorted array
2
3
4
5
6
Enter second sorted array
4
5
6
7
8
The merge array is: 2 3 4 5 6 7 8
--------------------------------
Process exited after 24.93 seconds with return value 0
Press any key to continue . . .