-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (128 loc) · 3.54 KB
/
main.cpp
File metadata and controls
167 lines (128 loc) · 3.54 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
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
// TestTaskC++.cpp: определяет точку входа для приложения.
//
#include "main.h"
#include "Figures/figures.h"
using namespace std;
bool FigureComp(const std::shared_ptr<Figure>& a, const std::shared_ptr<Figure>& b) {
return a->GetArea() < b->GetArea();
}
void PrintResult(vector<std::shared_ptr<Figure>>& arr, const string& msg)
{
string result = "\n\n"+msg+"\n";
for (const auto& figure : arr) {
result += (to_string(figure->GetArea()) + " ");
}
cout << result;
}
void merge(vector<std::shared_ptr<Figure>>& arr, int left, int mid, int right)
{
const int n1 = mid - left + 1;
const int n2 = right - mid;
vector<std::shared_ptr<Figure>> L(n1), R(n2);
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (L[i]->GetArea() <= R[j]->GetArea()) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void MergeSort(vector<std::shared_ptr<Figure>>& arr, int left, int right)
{
if (left >= right)
return;
int mid = left + (right - left) / 2;
MergeSort(arr, left, mid);
MergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
void StartMergeSort(vector<std::shared_ptr<Figure>> arr)
{
MergeSort(arr, 0, arr.size() - 1);
PrintResult(arr, "Merge Sort");
}
void BubbleSort(vector<std::shared_ptr<Figure>> figures)
{
int n = figures.size();
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (!FigureComp(figures[j], figures[j + 1]))
swap(figures[j], figures[j + 1]);
}
}
PrintResult(figures, "Bubble Sort");
}
int partition(vector<std::shared_ptr<Figure>>& arr, int low, int high) {
std::shared_ptr<Figure> pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j]->GetArea() < pivot->GetArea()) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void QuickSort(vector<std::shared_ptr<Figure>>& arr, int low, int high) {
if (low < high) {
// pi is the partition return index of pivot
int pi = partition(arr, low, high);
QuickSort(arr, low, pi - 1);
QuickSort(arr, pi + 1, high);
}
}
void StartQuickSort(vector<std::shared_ptr<Figure>> arr)
{
QuickSort(arr, 0, (arr.size() - 1)/2);
PrintResult(arr, "Quick Sort");
}
int main()
{
bool correctInit = true;
vector<std::shared_ptr<Figure>> figures;
try {
figures.push_back(std::make_shared<Circle>(36));
figures.push_back(std::make_shared<Triangle>(34, 25, 17));
figures.push_back(std::make_shared<Rectangle>(340, 25));
figures.push_back(std::make_shared<Triangle>(123.67, 146.43, 170.92));
figures.push_back(std::make_shared<Circle>(250));
}
catch(...){
cout << "Incorrectly initialized Figures";
correctInit = false;
}
if (correctInit) {
cout << "Threads started";
boost::thread t1(&StartMergeSort, figures);
boost::thread t2(&BubbleSort, figures);
boost::thread t3(&StartQuickSort, figures);
t1.join();
t2.join();
t3.join();
}
cout << "\n\n";
return 0;
}