-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.cpp
205 lines (167 loc) · 4.51 KB
/
Day04.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<<<<<<< HEAD
#include <iostream>
using namespace std;
// Function to find the next gap
int nextGap(int gap) {
if (gap <= 1)
return 0;
return (gap / 2) + (gap % 2);
}
// Function to swap two integers manually
void manualSwap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to merge two sorted arrays without extra space
void mergeArrays(int arr1[], int m, int arr2[], int n) {
int gap = m + n;
gap = nextGap(gap);
while (gap > 0) {
int i, j;
// Compare elements in the first array
for (i = 0; i + gap < m; i++) {
if (arr1[i] > arr1[i + gap]) {
manualSwap(&arr1[i], &arr1[i + gap]);
}
}
// Compare elements between the first and second array
for (j = (gap > m) ? gap - m : 0; i < m && j < n; i++, j++) {
if (arr1[i] > arr2[j]) {
manualSwap(&arr1[i], &arr2[j]);
}
}
// Compare elements in the second array
if (j < n) {
for (j = 0; j + gap < n; j++) {
if (arr2[j] > arr2[j + gap]) {
manualSwap(&arr2[j], &arr2[j + gap]);
}
}
}
gap = nextGap(gap);
}
}
// Helper function to print the array
void printArray(int arr[], int size) {
cout << "[";
for (int i = 0; i < size; i++) {
cout << arr[i];
if (i != size - 1)
cout << ", ";
}
cout << "]" << endl;
}
int main() {
int m, n;
// Input the size of the first array
cout << "Enter the size of arr1: ";
cin >> m;
// Input the size of the second array
cout << "Enter the size of arr2: ";
cin >> n;
// Declare the arrays
int arr1[m], arr2[n];
// Input elements for arr1
cout << "Enter elements of arr1 in sorted order: ";
for (int i = 0; i < m; i++) {
cin >> arr1[i];
}
// Input elements for arr2
cout << "Enter elements of arr2 in sorted order: ";
for (int i = 0; i < n; i++) {
cin >> arr2[i];
}
// Merge the arrays
mergeArrays(arr1, m, arr2, n);
// Output the merged arrays
cout << "arr1 = ";
printArray(arr1, m);
cout << "arr2 = ";
printArray(arr2, n);
return 0;
}
=======
#include <iostream>
using namespace std;
// Function to find the next gap
int nextGap(int gap) {
if (gap <= 1)
return 0;
return (gap / 2) + (gap % 2);
}
// Function to swap two integers manually
void manualSwap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to merge two sorted arrays without extra space
void mergeArrays(int arr1[], int m, int arr2[], int n) {
int gap = m + n;
gap = nextGap(gap);
while (gap > 0) {
int i, j;
// Compare elements in the first array
for (i = 0; i + gap < m; i++) {
if (arr1[i] > arr1[i + gap]) {
manualSwap(&arr1[i], &arr1[i + gap]);
}
}
// Compare elements between the first and second array
for (j = (gap > m) ? gap - m : 0; i < m && j < n; i++, j++) {
if (arr1[i] > arr2[j]) {
manualSwap(&arr1[i], &arr2[j]);
}
}
// Compare elements in the second array
if (j < n) {
for (j = 0; j + gap < n; j++) {
if (arr2[j] > arr2[j + gap]) {
manualSwap(&arr2[j], &arr2[j + gap]);
}
}
}
gap = nextGap(gap);
}
}
// Helper function to print the array
void printArray(int arr[], int size) {
cout << "[";
for (int i = 0; i < size; i++) {
cout << arr[i];
if (i != size - 1)
cout << ", ";
}
cout << "]" << endl;
}
int main() {
int m, n;
// Input the size of the first array
cout << "Enter the size of arr1: ";
cin >> m;
// Input the size of the second array
cout << "Enter the size of arr2: ";
cin >> n;
// Declare the arrays
int arr1[m], arr2[n];
// Input elements for arr1
cout << "Enter elements of arr1 in sorted order: ";
for (int i = 0; i < m; i++) {
cin >> arr1[i];
}
// Input elements for arr2
cout << "Enter elements of arr2 in sorted order: ";
for (int i = 0; i < n; i++) {
cin >> arr2[i];
}
// Merge the arrays
mergeArrays(arr1, m, arr2, n);
// Output the merged arrays
cout << "arr1 = ";
printArray(arr1, m);
cout << "arr2 = ";
printArray(arr2, n);
return 0;
}
>>>>>>> 733607e6d66e75f6545553e48563c1f9fe57d3a9