-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
123 lines (94 loc) · 2.4 KB
/
MergeSort.cpp
File metadata and controls
123 lines (94 loc) · 2.4 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
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
#include "MergeSort.h"
#include <chrono>
#include <thread>
#include <cassert>
#include <random>
#include <omp.h>
void MergeSort::mMerge(std::vector<int>& a, uint64_t beg, uint64_t m, uint64_t end)
{
std::vector<int> b(end - beg, 0);
uint64_t i = 0, j = beg, k = m;
while (j < m && k < end)
if (a[j] <= a[k])
b[i++] = a[j++];
else
b[i++] = a[k++];
while (j < m)
b[i++] = a[j++];
while(i>0) {
--i;
a[beg + i] = b[i];
}
}
void MergeSort::mSort(std::vector<int>& a, uint64_t beg, uint64_t end)
{
if (end - beg > 1) {
uint64_t m = (beg + end) >> 1;
mSort(a, beg, m);
mSort(a, m, end);
mMerge(a, beg, m, end);
}
}
void MergeSort::seqMSort(std::vector<int>& a)
{
mSort(a, 0, a.size());
}
void MergeSort::parMSort(std::vector<int>& a)
{
assert(a.size() % NO_OF_THREADS == 0);
// TODO implement parallel merge sort with std::thread
throw "NOT_IMPLEMENTED";
}
void MergeSort::ompMSort(std::vector<int>& a)
{
assert(a.size() % NO_OF_THREADS == 0);
// TODO implement parallel merge sort with OMP
throw "NOT_IMPLEMENTED";
}
uint64_t MergeSort::measuredSort(std::vector<int>& a, void(MergeSort::*sortFunc)(std::vector<int>& a))
{
auto start = std::chrono::high_resolution_clock::now();
(this->*sortFunc)(a);
auto stop = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
}
uint64_t MergeSort::seqMergeSort(std::vector<int>& a)
{
return measuredSort(a, &MergeSort::seqMSort);
}
uint64_t MergeSort::parMergeSort(std::vector<int>& a)
{
return measuredSort(a, &MergeSort::parMSort);
}
uint64_t MergeSort::ompMergeSort(std::vector<int>& a)
{
return measuredSort(a, &MergeSort::ompMSort);
}
bool MergeSort::prove(std::vector<int>& a)
{
for (uint64_t i = 0; i < a.size() - 1; ++i) {
if (a[i] > a[i + 1])
return false;
}
return true;
}
bool MergeSort::equal(std::vector<int>& a, std::vector<int>& b)
{
assert(a.size() == b.size());
bool ret = true;
for (int i = 0; i < a.size(); ++i) {
ret &= a[i] == b[i];
}
return ret;
}
std::vector<int> MergeSort::createRandomData(uint64_t size) {
std::vector<int> data(size, 0);
// random gen
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, (int)size);
generator.seed((unsigned)std::chrono::system_clock::now().time_since_epoch().count());
for (int i = 0; i < size; ++i) {
data[i] = distribution(generator);
}
return data;
}