-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSortedArray.cpp
78 lines (73 loc) · 1.86 KB
/
MergeSortedArray.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
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
// Time Complexity: O(n * mlog(m))
// Space Complexity: O(1)
// TLE
void mergeApproachOne(int arr1[], int arr2[], int n, int m) {
int j = 0;
for(int i = 0; i < n; i++) {
if(arr1[i] > arr2[0]) {
swap(arr1[i], arr2[0]);
sort(arr2, arr2 + m);
}
}
}
// GAP Algorithm
// Time Complexity: O(n+m * log2(n+m))
// Space Complexity: O(1)
// AC
int nextGap(int gap) {
if(gap <= 1)
return 0;
return (gap / 2) + (gap % 2);
}
void merge(int arr1[], int arr2[], int n, int m) {
int i, j, gap = n + m;
for (gap = nextGap(gap); gap > 0; gap = nextGap(gap)) {
for (i = 0; i + gap < n; i++) {
if (arr1[i] > arr1[i + gap]) {
swap(arr1[i], arr1[i + gap]);
}
}
for (j = gap > n ? gap - n : 0; j < m && i < n; j++, i++) {
if(arr1[i] > arr2[j]) {
swap(arr1[i], arr2[j]);
}
}
if(j < m) {
for (j = 0; j + gap < m; j++) {
if(arr2[j] > arr2[j + gap]) {
swap(arr2[j], arr2[j + gap]);
}
}
}
}
}
};
int main() {
int t;
cin >> t;
while (t--) {
int n, m, i;
cin >> n >> m;
int arr1[n], arr2[m];
for (i = 0; i < n; i++) {
cin >> arr1[i];
}
for (i = 0; i < m; i++) {
cin >> arr2[i];
}
Solution ob;
ob.merge(arr1, arr2, n, m);
for (i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
for (i = 0; i < m; i++) {
cout << arr2[i] << " ";
}
cout << "\n";
}
return 0;
}