-
Notifications
You must be signed in to change notification settings - Fork 0
/
task4.cpp
313 lines (240 loc) · 8.04 KB
/
task4.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <iostream>
#include <ctime>
#include <chrono>
class Sort{
public:
virtual int* sort(
int* array, std::size_t size,
std::size_t& swapCount,
std::size_t& comparationCount
) = 0;
protected:
void swap(int* arr, size_t i, size_t j){
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
_swapCount++;
}
std::size_t _swapCount = 0;
std::size_t _comparationCount = 0;
};
class InsertionSort : public Sort{
public:
int* sort(int *array, std::size_t size, std::size_t &swapCount, std::size_t &comparationCount) override {
for(size_t i = 1; i < size; i++) {
for (int j = i; j > 0 && array[j - 1] > array[j]; j--) {
swap(array, j - 1, j);
_comparationCount += 2;
}
_comparationCount++;
}
comparationCount = _comparationCount;
swapCount = _swapCount;
}
};
class SelectionSort : public Sort{
public:
int* sort(int *array, std::size_t size, std::size_t &swapCount, std::size_t &comparationCount) override {
for (std::size_t i = 0; i < size; i++) {
comparationCount++;
std::size_t maxIndex = 0;
int maxElem = array[0];
for(std::size_t j = 0; j <= size - i - 1; j++){
_comparationCount++;
if(array[j] > maxElem){
maxElem = array[j];
maxIndex = j;
}
_comparationCount++;
}
swap(array, maxIndex, size-i - 1);
}
comparationCount = _comparationCount;
swapCount = _swapCount;
}
};
class BubbleSort : public Sort{
public:
int* sort(int *array, std::size_t size, std::size_t &swapCount, std::size_t &comparationCount) override {
for (std::size_t i = 0; i < size; i++) {
comparationCount++;
for(std::size_t j = 0; j < size - i - 1; j++){
_comparationCount++;
if(array[j] > array[j+1]){
swap(array, j, j+1);
}
_comparationCount++;
}
}
comparationCount = _comparationCount;
swapCount = _swapCount;
}
};
class QuickSort : public Sort{
public:
int* sort(int *array, std::size_t size, std::size_t &swapCount, std::size_t &comparationCount) override {
quickSortRec(array, 0, size-1);
comparationCount = _comparationCount;
swapCount = _swapCount;
}
void quickSortRec(int arr[], int low, int high)
{
if (low < high)
{
_comparationCount++;
int pivot = partition(arr, low, high);
quickSortRec(arr, low, pivot - 1);
quickSortRec(arr, pivot + 1, high);
}
}
private:
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
_comparationCount++;
if (arr[j] <= pivot)
{
i++;
swap(arr, i, j);
}
_comparationCount++;
}
swap(arr, i + 1, high);
return (i + 1);
}
};
template<class T>
class Measurer {
public:
int* measure(int* arr, std::size_t size, std::size_t& swapCount, std::size_t& comparationCount, double& timeSpent){
auto start = std::chrono::high_resolution_clock::now();
sorter.sort(arr, size, swapCount, comparationCount);
auto end = std::chrono::high_resolution_clock::now();
timeSpent = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();;
}
private:
T sorter;
};
class Menu{
private:
enum Action {
INSERTIONSORT = 1,
SELECTIONSORT = 2,
BUBBLESORT = 3,
QUICKSORT = 4
};
friend std::istream& operator>>(std::istream &is, Action& action);
public:
static void printResult(int* arr, size_t arraySize, std::size_t swapCount,
std::size_t comparationCount,
double timeSpent){
std::cout << "Total time spent: " << timeSpent << std::endl;
std::cout << "Comparations made: " << comparationCount << std::endl;
std::cout << "Swaps made: " << swapCount << std::endl;
std::cout << "Sorted array: ";
for(size_t i = 0; i< arraySize; i++){
std::cout << arr[i] << ' ';
}
std::cout << std::endl;
}
static void printMenu(){
std::cout << "\t\tLaboratory work #4\n"
<< "Print 1 to measure complexity of Insertion sort\n"
<< "Print 2 to measure complexity of Selection sort\n"
<< "Print 3 to measure complexity of Bubble sort\n"
<< "Print 4 to measure complexity of Quick sort\n";
Action choice;
std::cin >> choice;
switch (choice) {
case INSERTIONSORT:{
std::size_t arraySize;
int* arr = getArray(arraySize);
Measurer<InsertionSort> measurer;
std::size_t swapCount, comparationCount;
double timeSpent;
measurer.measure(arr, arraySize, swapCount, comparationCount, timeSpent);
printResult(arr, arraySize, swapCount, comparationCount, timeSpent);
break;
}
case SELECTIONSORT:{
std::size_t arraySize;
int* arr = getArray(arraySize);
Measurer<SelectionSort> measurer;
std::size_t swapCount, comparationCount;
double timeSpent;
measurer.measure(arr, arraySize, swapCount, comparationCount, timeSpent);
printResult(arr, arraySize, swapCount, comparationCount, timeSpent);
break;
}
case BUBBLESORT:{
std::size_t arraySize;
int* arr = getArray(arraySize);
Measurer<BubbleSort> measurer;
std::size_t swapCount, comparationCount;
double timeSpent;
measurer.measure(arr, arraySize, swapCount, comparationCount, timeSpent);
printResult(arr, arraySize, swapCount, comparationCount, timeSpent);
break;
}
case QUICKSORT:{
std::size_t arraySize;
int* arr = getArray(arraySize);
Measurer<QuickSort> measurer;
std::size_t swapCount, comparationCount;
double timeSpent;
measurer.measure(arr, arraySize, swapCount, comparationCount, timeSpent);
printResult(arr, arraySize, swapCount, comparationCount, timeSpent);
break;
}
}
}
private:
static int* getArray(std::size_t& size){
std::cout << "Enter array size: ";
std::cin >> size;
int* arr = new int[size];
std::cout << "To enter array manually type 0, for random generation type 1: ";
int choice;
std::cin >> choice;
if(choice == 0){
for(std::size_t i = 0; i < size; i++){
std::cout << "Enter " << i+1 << " element: ";
std::cin >> arr[i];
}
}else if(choice == 1){
for(std::size_t i = 0; i < size; i++){
arr[i] = rand()/((RAND_MAX + 1u)/32000);
}
}
return arr;
}
};
std::istream& operator>>(std::istream &is, Menu::Action& action){
int inputNumber;
is >> inputNumber;
switch (inputNumber) {
case 1:{
action = Menu::Action::INSERTIONSORT;
return is;
}
case 2:{
action = Menu::Action::SELECTIONSORT;
return is;
}
case 3:{
action = Menu::Action::BUBBLESORT;
return is;
}
case 4:{
action = Menu::Action::QUICKSORT;
return is;
}
}
}
int main(){
std::srand(std::time(nullptr));
Menu::printMenu();
}