-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_sorted_arr_cons_space.cpp
119 lines (117 loc) · 1.51 KB
/
merge_sorted_arr_cons_space.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
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void merge(int a[], int b[], int n, int m)
{
int num = 0, p1 = 0, p2 = 0, i = 0;
for (int j = 0; j < n; j++)
{
num = max(num, a[j]);
}
for (int j = 0; j < m; j++)
{
num = max(num, b[j]);
}
num = max(num, n);
num = max(num, m);
num++;
while (p1 < n && p2 < m)
{
if (a[p1] > b[p2])
{
b[p2] = (b[p2] + (i * num));
p2++;
}
else
{
a[p1] = (a[p1] + (i * num));
p1++;
}
i++;
}
// cout<<"one"<<endl;
while (p2 < m)
{
b[p2] = (b[p2] + (i * num));
p2++;
i++;
}
// cout<<"sec"<<endl;
while (p1 < n)
{
a[p1] = (a[p1] + (i * num));
p1++;
i++;
}
// cout<<"third"<<endl;
i = 0;
while (i < n)
{
if ((a[i] / num) == i)
{
i++;
continue;
}
if ((a[i] / num) < n)
{
swap(&a[i], &a[a[i] / num]);
}
else
{
swap(&a[i], &b[(a[i] / num) - n]);
}
}
// cout<<"foutth"<<endl;
i = 0;
while (i < m)
{
if ((b[i] / num) == i + n)
{
i++;
continue;
}
if ((b[i] / num) < n)
{
swap(&b[i], &a[b[i] / num]);
}
else
{
swap(&b[i], &b[(b[i] / num) - n]);
}
}
i = 0;
while (i < n + m)
{
if (i < n)
{
a[i] = a[i] % num;
}
else
{
b[i - n] %= num;
}
i++;
}
// cout<<"five"<<endl;
}
int main()
{
int n, m;
int arr1[] = {1, 3, 5, 9};
int arr2[] = {2, 4, 6, 7, 8};
merge(arr1, arr2, 4, 5);
for (int i = 0; i < 4; i++)
{
cout << arr1[i] << " ";
}
for (int i = 0; i < 5; i++)
{
cout << arr2[i] << " ";
}
return 0;
}