-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerging_algorithm.c
More file actions
50 lines (48 loc) · 1004 Bytes
/
merging_algorithm.c
File metadata and controls
50 lines (48 loc) · 1004 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
#include <stdio.h>
void merge(int a[],int b[],int c[],int n,int m,int sum);
int main(){
int a[10],b[10],c[20],i,j,k,n,m,sum;
printf("Please input the size of A : ");
scanf("%d",&n);
printf("Please input the element of A : ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Please input the size of B : ");
scanf("%d",&m);
sum=n+m;
printf("Please input the element of B : ");
for(i=0;i<m;i++){
scanf("%d",&b[i]);
}
merge(a,b,c,n,m,sum);
printf("Array C is : ");
for(i=0;i<sum;i++){
printf("%d ",c[i]);
}
return 0;
}
void merge(int a[],int b[],int c[],int n,int m,int sum){
int i=0,j=0,k=0;
while(i<n && j<m){
if(a[i]<=b[j]){
c[k]=a[i];
i++;
}
else {
c[k]=b[j];
j++;
}
k++;
}
while(i<n){
c[k]=a[i];
i++;
k++;
}
while(j<m){
c[k]=b[j];
j++;
k++;
}
}