-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc19.c
More file actions
64 lines (58 loc) · 1.41 KB
/
c19.c
File metadata and controls
64 lines (58 loc) · 1.41 KB
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
// Merge two sorted arrays without using extra space
#include <stdio.h>
int main() {
int arr1[] = {1, 3, 5, 9};
int arr2[] = {2, 4, 6, 8};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
int i = 0, j = 0, k = n1 - 1;
while (i <= k && j < n2) {
if (arr1[i] < arr2[j]) {
i++;
} else {
// Swap arr1[k] and arr2[j]
int temp = arr1[k];
arr1[k] = arr2[j];
arr2[j] = temp;
k--;
j++;
}
}
// Sort both arrays
for (int x = 0; x < n1 - 1; x++)
{
for (int y = 0; y < n1 - x - 1; y++)
{
if (arr1[y] > arr1[y + 1])
{
int temp = arr1[y];
arr1[y] = arr1[y + 1];
arr1[y + 1] = temp;
}
}
}
for (int x = 0; x < n2 - 1; x++)
{
for (int y = 0; y < n2 - x - 1; y++)
{
if (arr2[y] > arr2[y + 1])
{
int temp = arr2[y];
arr2[y] = arr2[y + 1];
arr2[y + 1] = temp;
}
}
}
// Print merged arrays
printf("First Array: ");
for (i = 0; i < n1; i++)
{
printf("%d ", arr1[i]);
}
printf("\nSecond Array: ");
for (j = 0; j < n2; j++)
{
printf("%d ", arr2[j]);
}
return 0;
}