-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
162 lines (133 loc) · 5.98 KB
/
main.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 <fstream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>
#include "InsertionSort.hpp"
#include "HeapSort.hpp"
#include "QuickSort.hpp"
#include "RadixSort.hpp"
#include "HybridSort.hpp"
using ns = std::chrono::nanoseconds;
std::vector<int> generateRandomList(int size) {
std::vector<int> list;
list.reserve(size);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, size);
for (int i = 0; i < size; ++i) {
list.push_back(dist(gen));
}
return list;
}
ns calculateSorting(std::vector<int>& list, void(*sortingAlgorithm)(std::vector<int>&, int, int)) {
std::vector<int> copy(list.size());
std::copy(list.begin(), list.end(), copy.begin());
auto start = std::chrono::steady_clock::now();
sortingAlgorithm(copy, 0, copy.size() - 1);
auto end = std::chrono::steady_clock::now();
return std::chrono::duration_cast<ns>(end - start);
}
void runMeasurement(int sizeStep, int maxSize, int maxTimes, std:: ofstream& file_out) {
file_out << "Size Insertion Heap Quick RadixLSD Hybrid InsertionSorted HeapSorted QuickSorted RadixLSDSorted HybridSorted InsertionEqual HeapEqual QuickEqual RadixLSDEqual HybridEqual InsertionReversed HeapReversed QuickReversed RadixLSDReversed HybridReversed" << std:: endl;
for (int size = sizeStep; size <= maxSize; size += sizeStep) {
if (size == 100) {
sizeStep = 100;
} else if (size == 1000) {
sizeStep = 500;
} else if (size == 10000) {
sizeStep = 1000;
}
ns total_time_insertion = ns(0);
ns total_time_heap = ns(0);
ns total_time_quick = ns(0);
ns total_time_radix = ns(0);
ns total_time_hybrid = ns(0);
ns total_time_insertion_sorted = ns(0);
ns total_time_heap_sorted = ns(0);
ns total_time_quick_sorted = ns(0);
ns total_time_radix_sorted = ns(0);
ns total_time_hybrid_sorted = ns(0);
ns total_time_insertion_reversed = ns(0);
ns total_time_heap_reversed = ns(0);
ns total_time_quick_reversed = ns(0);
ns total_time_radix_reversed = ns(0);
ns total_time_hybrid_reversed = ns(0);
ns total_time_insertion_part = ns(0);
ns total_time_heap_part = ns(0);
ns total_time_quick_part = ns(0);
ns total_time_radix_part = ns(0);
ns total_time_hybrid_part = ns(0);
for (int times = 0; times < maxTimes; ++times) {
std::vector<int> list = generateRandomList(size);
total_time_insertion += calculateSorting(list, insertionSort);
total_time_heap += calculateSorting(list, heapSort);
total_time_quick += calculateSorting(list, quickSort);
total_time_radix += calculateSorting(list, radixSortLSD);
total_time_hybrid += calculateSorting(list, hybridSort);
for (int i = 0; i < size; ++i) {
list[i] = i;
}
total_time_insertion_sorted += calculateSorting(list, insertionSort);
total_time_heap_sorted += calculateSorting(list, heapSort);
total_time_quick_sorted += calculateSorting(list, quickSort);
total_time_radix_sorted += calculateSorting(list, radixSortLSD);
total_time_hybrid_sorted += calculateSorting(list, hybridSort);
std::reverse(list.begin(), list.end());
total_time_insertion_reversed += calculateSorting(list, insertionSort);
total_time_heap_reversed += calculateSorting(list, heapSort);
total_time_quick_reversed += calculateSorting(list, quickSort);
total_time_radix_reversed += calculateSorting(list, radixSortLSD);
total_time_hybrid_reversed += calculateSorting(list, hybridSort);
for (int i = 0; i < size; ++i) {
list[i] = i;
}
std::swap(list[0], list[list.size()]);
total_time_insertion_part += calculateSorting(list, insertionSort);
total_time_heap_part += calculateSorting(list, heapSort);
total_time_quick_part += calculateSorting(list, quickSort);
total_time_radix_part += calculateSorting(list, radixSortLSD);
total_time_hybrid_part += calculateSorting(list, hybridSort);
}
file_out << size << " "
<< total_time_insertion.count() / maxTimes << " "
<< total_time_heap.count() / maxTimes << " "
<< total_time_quick.count() / maxTimes << " "
<< total_time_radix.count() / maxTimes << " "
<< total_time_hybrid.count() / maxTimes << " "
<< total_time_insertion_sorted.count() / maxTimes << " "
<< total_time_heap_sorted.count() / maxTimes << " "
<< total_time_quick_sorted.count() / maxTimes << " "
<< total_time_radix_sorted.count() / maxTimes << " "
<< total_time_hybrid_sorted.count() / maxTimes << " "
<< total_time_insertion_part.count() / maxTimes << " "
<< total_time_heap_part.count() / maxTimes << " "
<< total_time_quick_part.count() / maxTimes << " "
<< total_time_radix_part.count() / maxTimes << " "
<< total_time_hybrid_part.count() / maxTimes << " "
<< total_time_insertion_reversed.count() / maxTimes << " "
<< total_time_heap_reversed.count() / maxTimes << " "
<< total_time_quick_reversed.count() / maxTimes << " "
<< total_time_radix_reversed.count() / maxTimes << " "
<< total_time_hybrid_reversed.count() / maxTimes
<< std::endl;
}
}
int main() {
const int maxSize = 20000;
const int maxTimes = 100;
const int sizeStep = 10;
std::ofstream file_out("final_results3.txt");
if (!file_out.is_open()) {
std::cerr << "Error opening file." << std::endl;
return 1;
}
ns total_time_take_to_run = ns(0);
auto start = std::chrono::steady_clock::now();
runMeasurement(sizeStep, maxSize, maxTimes, file_out);
auto end = std::chrono::steady_clock::now();
total_time_take_to_run = std::chrono::duration_cast<ns>(end-start);
std::cout << total_time_take_to_run.count() << std::endl;
return 0;
}