-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerging.cpp
More file actions
61 lines (57 loc) · 815 Bytes
/
Merging.cpp
File metadata and controls
61 lines (57 loc) · 815 Bytes
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
53
54
55
56
57
58
59
60
/* merging */
#include<stdio.h>
void merge(int [],int [],int,int,int [],int);
main()
{
int a[50],b[50],c[50],m,n,i,k;
printf("enter the no of terms of the 1st array ");
scanf("%d",&m);
printf("enter the elements\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("enter the no of terms of the 2nd array ");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
k=m+n;
merge(a,b,m,n,c,k);
return(0);
}
void merge(int a[],int b[],int m,int n,int c[],int k)
{
int i,j,l;
i=0;j=0;l=0;
while(i<m && j<n)
if(a[i]<b[j])
{
c[l]=a[i];
i++;
l++;
}
else
{
c[l]=b[j];
j++;
l++;
}
while(i<m)
{
c[l++]=a[i];
i++;
}
while(j<n)
{
c[l++]=b[j];
j++;
}
printf("the merged elements are\n");
for(i=0;i<k;i++)
{
printf("%d\t",c[i]);
}
}