-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_array.c
53 lines (52 loc) · 945 Bytes
/
merge_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
void merge(int a[],int b[],int c[],int m,int n);
void main()
{
int m,n,i,a[20],b[20],c[20];
printf("\nEnter the size of first array:");
scanf("%d",&m);
printf("Enter the elements of array:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("\nEnter the size of second array:");
scanf("%d",&n);
printf("Enter the elements of array:");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
merge(a,b,c,m,n);
}
void merge(int a[],int b[],int c[],int m,int n)
{
int i,j,k;
i=j=k=0;
while(i<m&&j<n)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=b[j];
j++;
k++;
}
}
while(i<m)
{
c[k]=a[j];
k++;
i++;
}
while(j<n)
{
c[k]=b[j];
k++;
j++;
}
printf("Merged array is:\n");
for(i=0;i<k;i++)
printf("%d",c[i]);
}