-
Notifications
You must be signed in to change notification settings - Fork 0
/
divide_and_conquer.cpp
162 lines (116 loc) · 2.93 KB
/
divide_and_conquer.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
#include <iostream>
#include <vector>
#include <ctime>
template<typename T>
void merge(std::vector<T>& a, int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
std::vector<T> left(n1 + 1);
std::vector<T> right(n2 + 1);
for (int i = 0; i < n1; ++i)
left[i] = std::move(a[p + i]);
for (int j = 0; j < n2; ++j)
right[j] = std::move(a[q + j + 1]);
left[n1] = 999999999;
right[n2] = 999999999;
int i = 0;
int j = 0;
for (int k = p; k <= r; ++k)
{
if (left[i] <= right[j])
a[k] = std::move(left[i++]);
else
a[k] = std::move(right[j++]);
}
}
template<typename T>
void mergeSort(std::vector<T>& a, int p, int r)
{
//std::cout << "p=" << p << " r = " << r << std::endl;
if (p < r)
{
int q = (p + r) / 2; // divide step
mergeSort(a, p, q);
mergeSort(a, q + 1, r);
//std::cout << "merging, p=" << p << " q = " << q << " r = " << r << std::endl;
merge(a, p, q, r);
}
}
template<typename Comparable>
void merge2(std::vector<Comparable>& a,
std::vector<Comparable>& tmpArray, int leftPos, int rightPos, int rightEnd)
{
int leftEnd = rightPos - 1;
int tmpPos = leftPos;
int numElements = rightEnd - leftPos + 1;
while (leftPos <= leftEnd && rightPos <= rightEnd)
if (a[leftPos] <= a[rightPos])
tmpArray[tmpPos++] = std::move(a[leftPos++]);
else
tmpArray[tmpPos++] = std::move(a[rightPos++]);
while (leftPos <= leftEnd) // Copy rest of first half
tmpArray[tmpPos++] = std::move(a[leftPos++]);
while (rightPos <= rightEnd) // Copy rest of right half
tmpArray[tmpPos++] = std::move(a[rightPos++]);
// Copy tmpArray back
for (int i = 0; i < numElements; ++i, --rightEnd)
a[rightEnd] = std::move(tmpArray[rightEnd]);
}
template<typename Comparable>
void mergeSort2(std::vector<Comparable>& a,
std::vector<Comparable>& tmpArray, int left, int right)
{
if (left < right)
{
int center = (left + right) / 2;
mergeSort2(a, tmpArray, left, center);
mergeSort2(a, tmpArray, center + 1, right);
merge2(a, tmpArray, left, center + 1, right);
}
}
template<typename Comparable>
void mergeSort2(std::vector<Comparable>& a)
{
std::vector<Comparable> tmpArray(a.size());
mergeSort2(a, tmpArray, 0, a.size() - 1);
}
template<typename T>
void shellSort(std::vector<T>& a, std::vector<int> gaps)
{
for (int gap : gaps)
{
for (int i = gap; i < a.size(); ++i)
{
T tmp = std::move(a[i]);
int j = i;
for (; j >= gap && tmp < a[j - gap]; j -= gap)
a[j] = std::move(a[j - gap]);
a[j] = std::move(tmp);
}
}
}
int main()
{
int sz = 10000000;
std::vector<int> a(sz);
for (int i = 0; i < sz; ++i)
a[i] = sz - i;
std::vector<int> hibbardGaps{};
for (int i = sz / 2; i /= 2; i > 1)
hibbardGaps.push_back(i - 1);
std::clock_t start;
double duration;
start = std::clock();
//mergeSort2(a);
//mergeSort(a, 0, a.size()-1);
shellSort(a, hibbardGaps);
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
std::cout << "printf: " << duration << '\n';
}
/*
*/
/*
*/
/*
*/