Friday, 16 May 2014

Merging Two List Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,m,n,a[20],b[20],c[20];
clrscr();
printf("$$$ MERGING TWO LIST USING ARRAY $$$");
printf("\nEnter the size of the first array:");
scanf("%d",&n);
printf("Enter first sorted array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the size of the second array:");
scanf("%d",&m);
printf("Enter second sorted array elements:");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
i=j=k=0;
while(i<n && j<m)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=b[j];
k++;
j++;
}
}
while(i<n)
{
c[k]=a[i];
k++;
i++;
}
while(j<m)
{
c[k]=b[j];
k++;
j++;
}
printf("Merging of two sorted array is:");
for(k=0;k<n+m;k++)
{
printf("%d\t",c[k]);
}
getch();
}

OUTPUT:

$$$ MERGING TWO LIST USING ARRAY $$$

Enter the size of the first array:4
Enter first sorted array elements:    12    23    35    47

Enter the size of the second array:3
Enter second sorted array elements:    27    68    77

Merging of two sorted array is:    12       23      27      35      47      68     77

No comments:

Post a Comment