-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
215 lines (195 loc) · 5.71 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
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
#include <bits/stdc++.h>
#include <fstream>
#include <chrono>
using namespace std;
ifstream fin("date.in");
int n, *v, *copie, baza, nmax, *aux;
void citire_elemente() {
int i;
fin >> n >> nmax >> baza;
v = new int[n];
copie = new int[n];
srand((unsigned) time(NULL));
for (i = 0; i < n; i += 1) {
v[i] = copie[i] = rand() % nmax;
}
}
void copiere() {
int i;
for (i = 0; i < n; i += 1)
v[i] = copie[i];
}
int val(int nr, int putere) {
return (nr >> putere) & (baza - 1);
}
void RadixSort() {
long long nr;
int i, maxi, *aparitii, *afis, putere = 0, d;
maxi = v[0];
for (i = 1; i < n; i += 1) {
if (v[i] > maxi)
maxi = v[i];
}
d = int(log2(baza));
aparitii = new int[baza];
afis = new int[n];
for (nr = 1; maxi >= nr; nr *= baza) {
for (i = 0; i < baza; i += 1) {
aparitii[i] = 0;
}
for (i = 0; i < n; i += 1) {
afis[i] = 0;
}
for (i = 0; i < n; i += 1) {
aparitii[val(v[i], putere)] += 1;
}
for (i = 1; i < baza; i += 1) {
aparitii[i] += aparitii[i - 1];
}
for (i = n - 1; i >= 0; i -= 1) {
afis[--aparitii[val(v[i], putere)]] = v[i];
}
for (i = 0; i < n; i += 1)
v[i] = afis[i];
putere += d;
}
}
void CountingSort() {
int maxi, i, val_relativa, *aparitii, *sortat;
maxi = -(1 << 30);
for (i = 0; i < n; i += 1) {
if (v[i] > maxi)
maxi = v[i];
}
aparitii = new int[maxi + 1];
sortat = new int[n];
for (i = 0; i <= maxi; i += 1)
aparitii[i] = 0;
for (i = 0; i < n; i += 1)
aparitii[v[i]]++;
for (i = 1; i <= maxi; i += 1)
aparitii[i] += aparitii[i - 1];
for (i = 0; i < n; i += 1) {
val_relativa = aparitii[v[i]] - 1;
sortat[val_relativa] = v[i];
aparitii[v[i]] -= 1;
}
for (i = 0; i < n; i += 1)
v[i] = sortat[i];
}
void QuickSort(int st, int dr) {
if (st < dr) {
int pivot, i, j;
pivot = v[dr];
i = st - 1;
j = st;
while (j < dr) {
if (v[j] <= pivot) {
i++;
if (i != j) {
v[i] = v[i] ^ v[j];
v[j] = v[i] ^ v[j];
v[i] = v[i] ^ v[j];
}
}
j += 1;
}
if (i + 1 != dr) {
v[i + 1] = v[i + 1] ^ v[dr];
v[dr] = v[i + 1] ^ v[dr];
v[i + 1] = v[i + 1] ^ v[dr];
}
QuickSort(st, i);
QuickSort(i + 2, dr);
}
}
void ShellSort() {
int interval = n >> 1, sublista, pivot, i;
while (interval) {
for (sublista = interval; sublista < n; sublista += 1) {
pivot = v[sublista];
for (i = sublista; i >= interval; i -= interval) {
if (v[i - interval] > pivot) {
v[i] = v[i - interval];
} else break;
}
v[i] = pivot;
}
interval = interval >> 1;
}
}
void Merge(int st, int mij, int dr) {
int i, j, m = 0;
i = st;
j = mij + 1;
while (i <= mij && j <= dr) {
if (v[i] <= v[j])
aux[m++] = v[i++];
if (v[j] < v[i])
aux[m++] = v[j++];
}
while (i <= mij)
aux[m++] = v[i++];
while (j <= dr)
aux[m++] = v[j++];
i = st;
j = 0;
while (i <= dr) {
v[i++] = aux[j++];
}
}
void MergeSort(int st, int dr) {
int mij;
if (st < dr) {
mij = st + (dr - st) / 2;
MergeSort(st, mij);
MergeSort(mij + 1, dr);
Merge(st, mij, dr);
}
}
int main() {
int i, T, x;
fin >> T;
for (i = 0; i < T; i += 1) {
citire_elemente();
auto inceput3 = std::chrono::steady_clock::now();
ShellSort();
auto sfarsit3 = std::chrono::steady_clock::now();
std::chrono::duration<double> secunde3 = sfarsit3 - inceput3;
std::cout << "Timpul pentru Shell Sort este de: " << secunde3.count() << "s\n";
copiere();
aux = new int[n];
auto inceput2 = std::chrono::steady_clock::now();
MergeSort(0, n - 1);
auto sfarsit2 = std::chrono::steady_clock::now();
std::chrono::duration<double> secunde2 = sfarsit2 - inceput2;
std::cout << "Timpul pentru Merge Sort este de: " << secunde2.count() << "s\n";
copiere();
auto inceput4 = std::chrono::steady_clock::now();
CountingSort();
auto sfarsit4 = std::chrono::steady_clock::now();
std::chrono::duration<double> secunde4 = sfarsit4 - inceput4;
std::cout << "Timpul pentru Counting Sort este de: " << secunde4.count() << "s\n";
copiere();
auto inceput5 = std::chrono::steady_clock::now();
QuickSort(0, n - 1);
auto sfarsit5 = std::chrono::steady_clock::now();
std::chrono::duration<double> secunde5 = sfarsit5 - inceput5;
std::cout << "Timpul pentru Quick Sort este de: " << secunde5.count() << "s\n";
copiere();
auto inceput6 = std::chrono::steady_clock::now();
sort(v, v + n);
auto sfarsit6 = std::chrono::steady_clock::now();
std::chrono::duration<double> secunde6 = sfarsit6 - inceput6;
std::cout << "Timpul pentru Sort este de: " << secunde6.count() << "s\n";
copiere();
auto inceput1 = std::chrono::steady_clock::now();
RadixSort();
auto sfarsit1 = std::chrono::steady_clock::now();
std::chrono::duration<double> secunde1 = sfarsit1 - inceput1;
std::cout << "Timpul pentru Radix Sort este de: " << secunde1.count() << "s\n";
copiere();
cout << endl;
}
return 0;
}