-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path11_merge.c
57 lines (47 loc) · 1.42 KB
/
11_merge.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
54
55
56
57
// // Write a program in C to merge two sorted arrays and store result in another array.
// // Header Files
#include <stdio.h>
#include <conio.h>
// // Main Function Start
int main()
{
// // Declare Arrays
int arr1[] = {3, 10, 45, 68, 234}, arr2[] = {4, 5, 9, 224, 501, 555, 1000};
int size1 = sizeof(arr1) / sizeof(arr1[0]), size2 = sizeof(arr2) / sizeof(arr2[0]), size3 = size1 + size2;
int merged[size3];
// // Merge arrays
int i = 0, j = 0, k = 0;
while (i < size1 && j < size2)
{
if (arr1[i] < arr2[j])
merged[k++] = arr1[i++];
else
merged[k++] = arr2[j++];
}
// // append rest of the elements if available
while (i < size1)
{
merged[k++] = arr1[i++];
}
// // append rest of the elements if available
while (j < size2)
{
merged[k++] = arr2[j++];
}
// // Print Elements of Array-1
puts("\n\n>>>>>>>> Elements of Array-1 <<<<<<<<<");
for (int i = 0; i < size1; i++)
printf("%d ", arr1[i]);
// // Print Elements of Array-1
puts("\n\n>>>>>>>> Elements of Array-2 <<<<<<<<<");
for (int i = 0; i < size2; i++)
printf("%d ", arr2[i]);
// // Print Elements of Merged Array
puts("\n\n>>>>>>>> Elements of Merged Array <<<<<<<<<");
for (int i = 0; i < size3; i++)
printf("%d ", merged[i]);
putch('\n');
getch();
return 0;
}
// // Main Function End