-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge_2.c
53 lines (46 loc) · 1.39 KB
/
merge_2.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
53
// Merge two sorted array...
#include<stdio.h>
int main() {
int a[10], b[10], c[20];
int n1, n2;
printf("Enter the size of the first array: ");
scanf("%d", &n1);
printf("Enter the elements of the first array: ");
for(int i = 0; i < n1; i++) scanf("%d", &a[i]);
printf("Enter the size of the second array: ");
scanf("%d", &n2);
printf("Enter the elements of the second array: ");
for(int i = 0; i < n2; i++) scanf("%d", &b[i]);
int m = n1+n2;
int index = 0, index_first = 0, index_second = 0;
while(index_first < n1 && index_second < n2) {
if(a[index_first] < b[index_second]) {
c[index] = a[index_first];
index_first++;
}
else {
c[index] = b[index_second];
index_second++;
}
index++;
}
// elements of the first array is over but second array elements are remaind
if(index_first == n1) {
while(index_second < n2) {
c[index] = b[index_second];
index_second++;
index++;
}
}
// Elements of the second array is over but first array is remaind
else if(index_second == n2) {
while(index_first < n1) {
c[index] = a[index_first];
index_first++;
index++;
}
}
// printing the array...
for(int i = 0; i < m; i++) printf("%d ", c[i]);
return 0;
}