-
Notifications
You must be signed in to change notification settings - Fork 16
/
14.c
74 lines (60 loc) · 1.36 KB
/
14.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
void mergeArrays(int array1[], int size1, int array2[], int size2, int mergedArray[])
{
int i = 0, j = 0, k = 0;
while (i < size1 && j < size2)
{
if (array1[i] <= array2[j])
{
mergedArray[k] = array1[i];
i++;
}
else
{
mergedArray[k] = array2[j];
j++;
}
k++;
}
while (i < size1)
{
mergedArray[k] = array1[i];
i++;
k++;
}
while (j < size2)
{
mergedArray[k] = array2[j];
j++;
k++;
}
}
int main()
{
int size1, size2;
printf("Enter the size of the first array: ");
scanf("%d", &size1);
int array1[size1];
printf("Enter the elements of the first array:\n");
for (int i = 0; i < size1; i++)
{
scanf("%d", &array1[i]);
}
printf("Enter the size of the second array: ");
scanf("%d", &size2);
int array2[size2];
printf("Enter the elements of the second array:\n");
for (int i = 0; i < size2; i++)
{
scanf("%d", &array2[i]);
}
int mergedArray[size1 + size2];
mergeArrays(array1, size1, array2, size2, mergedArray);
printf("Sorted list after merging the arrays:\n");
for (int i = 0; i < size1 + size2; i++)
{
printf("%d ", mergedArray[i]);
}
printf("\n");
return 0;
}