C Programming

Merging two sorted arrays

Program to merge two sorted arrays in C

Language : C

[sourcecode language='c']
//Merging of two Sorted Arrays
#include
#include

void main()
{
int a[10],b[10],c[20],i,j,x,y,z;
clrscr();
printf(“Enter the size of 1st Array\t”);
scanf(“%d”,&x);
printf(“\nEnter the elements of 1st Array\n”);
for(i=0;i {
scanf(“%d”,&a[i]);
}

printf(“\nEnter the size of 2nd Array\t”);
scanf(“%d”,&y);
printf(“\nEnter the elements of 2nd Array\n”);
for(j=0;j {
scanf(“%d”,&b[j]);
}
i=0,j=0,z=0;

while((i {
if(a[i]<=b[j])
{
c[z]=a[i];
i++;
}

else
{
c[z]=b[j];
j++;
}

z++;

}

while(i {
c[z]=a[i];
z++;
i++;
}

while(j {
c[z]=b[j];
z++;
j++;
}

printf(“\n The merged array:”);
for(i=0;i {
printf(“%d\t”,c[i]);
}

}
[/sourcecode]



Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal