forked from Ite-2022-pwr/sem4-aizo-proj1-jn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
149 lines (104 loc) · 3.59 KB
/
QuickSort.cpp
File metadata and controls
149 lines (104 loc) · 3.59 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
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
#include <type_traits>
#include <stdlib.h>
#include <time.h>
#include <stdexcept>
#include "QuickSort.h"
using namespace std;
template <typename T>
QuickSort<T>::QuickSort() {
static_assert(std::is_same<T, char>::value || std::is_same<T, int>::value || std::is_same<T, float>::value,
"Invalid type. Only char, int, and float are allowed.");
}
template <typename T>
T* QuickSort<T>::sorted(T* data, int size, int left, int right, pivotIndex index) {
// skopiuj pocz¹tkowe wartoœci z rozwa¿anej tablicy
T* initial_data = new T[size];
for (int i = 0; i < size; i++)
initial_data[i] = data[i];
this->sort(initial_data, left, right, index);
return initial_data;
}
template <typename T>
void QuickSort<T>::sort(T* data, int left, int right, pivotIndex index) {
if (left < right) {
int s = partition(data, left, right, index);
this->sort(data, left, s, index);
this->sort(data, s + 1, right, index);
}
}
template <typename T>
int QuickSort<T>::partition(T* data, int left, int right, pivotIndex index)
{
int pivot = data[left];
srand(time(NULL));
if (index == LAST)
pivot = data[right];
else if (index == MIDDLE)
pivot = data[(int)((left + right) / 2)];
else if (index == RANDOM)
pivot = data[left + (rand() % (right - left + 1))];
int l = left; int r = right;
while (1) {
while (data[l] < pivot) ++l;
while (data[r] > pivot) --r;
if (l < r) {
T temp = data[l];
data[l] = data[r];
data[r] = temp;
++l;
--r;
}
else {
if (r == right) r--;
return r;
}
}
}
template <typename T>
void QuickSort<T>::swap(T* first, T* second) {
T temp = *first;
*first = *second;
*second = temp;
}
template <typename T>
void QuickSort<T>::measure_sorting(int no_times, int size, pivotIndex index) {
double avg_duration = 0;
// tyle razy ile zadano, sortuj i zmierz czas
for (int _ = 0; _ < no_times; _++) {
T* ptr = this->generate_random_array(size);
chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
this->sort(ptr, 0, size - 1, index);
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
double sort_duration = chrono::duration_cast<chrono::duration<double>>(t2 - t1).count();
cout << "sorting took: " << sort_duration << "s" << endl;
avg_duration += sort_duration;
}
cout << "average sorting time: " << avg_duration / no_times << "s" << endl;
}
template <typename T>
void QuickSort<T>::measure_sorting(int no_times, T* data, int size, pivotIndex index) {
double avg_duration = 0;
// tyle razy ile zadano, sortuj i zmierz czas
for (int _ = 0; _ < no_times; _++) {
// skopiuj pocz¹tkowe wartoœci z rozwa¿anej tablicy
T* initial_data = new T[size];
for (int i = 0; i < size; i++)
initial_data[i] = data[i];
chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
this->sort(initial_data, 0, size - 1, index);
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
if (!this->is_sorted(initial_data, size, true))
cout << "NOT SORTED!" << endl;
double sort_duration = chrono::duration_cast<chrono::duration<double>>(t2 - t1).count();
cout << "sorting took: " << sort_duration << "s" << endl;
avg_duration += sort_duration;
}
cout << "average sorting time: " << avg_duration / no_times << "s" << endl;
}
template class QuickSort<int>;
template class QuickSort<float>;
template class QuickSort<char>;