-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_test.cpp
268 lines (226 loc) · 5.54 KB
/
sort_test.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
/**
* @file sort_test.cpp
*
* Practicas de PROA
* Practica 2.
*
* @author Ignacio Gomis Lli
* @author Juan Pablo Uriol Balbin
* @date 28/09/2017
* @version 1.0
*/
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <random>
#include <math.h>
#include <time.h>
using namespace std;
bool RandomVector(vector<int> &, int, int, mt19937);
void CountSort(vector<int> &, int);
void LSDRadixSortVector(vector<int> &, int);
void ConcatenarVector(vector<int> &, const vector<int> &);
int Digito(int, int);
bool RandomList(list<int> &, int, int, mt19937);
void LSDRadixSortLista(list<int> &, int);
/**
*
* Funcion Main
* Ejecuta las pruebas variando el metodo de ordenado.
*
*/
int main() {
double BILLION = 1000000000;
int LOW = 0;
int UPP1 = 65535;
vector<int> vect(10000);
list<int> lista(10000);
vector<int> vectAux(10000);
list<int> listaAux(10000);
struct timespec ini_time,fin_time;
srand(time(NULL));
const int SEED = rand();
std::mt19937 aleatorio(SEED);
double total;
cout<<endl;
clock_gettime(CLOCK_REALTIME, &ini_time);
for (int i = 0; i < 1000; i++) {
RandomVector(vect, LOW, UPP1, aleatorio);
vectAux = vect;
CountSort(vectAux, UPP1);
}
clock_gettime(CLOCK_REALTIME, &fin_time);
total = (fin_time.tv_sec - ini_time.tv_sec)
+ (fin_time.tv_nsec - ini_time.tv_nsec) / BILLION;
cout << "Tiempo medio Countsort: " << total / 1000<<endl;
clock_gettime(CLOCK_REALTIME, &ini_time);
for (int i = 0; i < 1000; i++) {
RandomVector(vect, LOW, UPP1, aleatorio);
vectAux = vect;
LSDRadixSortVector(vectAux, UPP1);
}
clock_gettime(CLOCK_REALTIME, &fin_time);
total = (fin_time.tv_sec - ini_time.tv_sec)
+ (fin_time.tv_nsec - ini_time.tv_nsec) / BILLION;
cout << "Tiempo medio RadixSort Vector: " << total / 1000<<endl;
clock_gettime(CLOCK_REALTIME, &ini_time);
for (int i = 0; i < 1000; i++) {
RandomList(lista, LOW, UPP1, aleatorio);
listaAux = lista;
LSDRadixSortLista(listaAux, UPP1);
}
clock_gettime(CLOCK_REALTIME, &fin_time);
total = (fin_time.tv_sec - ini_time.tv_sec)
+ (fin_time.tv_nsec - ini_time.tv_nsec) / BILLION;
cout << "Tiempo medio RadixSort Lista: " << total / 1000<<endl;
return 0;
}
/**
*
* Generara un vector de enteros aleatorios
*
* @param[out] vect Vector de enteros con tamaño asignado
* @param[in] lowlim Limite inferior aleatorio
* @param[in] uplim Limite superior aleatorio
* @return True if lowlim <= uplim False if lowlim>uplim
*/
bool RandomVector(vector<int> & vect, int lowlim, int uplim,
mt19937 aleatorio) {
if (lowlim <= uplim) {
for (unsigned i = 0; i < vect.size(); i++) {
vect[i] = ((aleatorio() % (uplim - lowlim + 1)) + lowlim);
}
return true;
} else
return false;
}
/**
*
* Aplicara el algoritmo CountSort sobre un vector
*
*
* @param vect Vector a ordenar
* @param max Numero maximo que puede alcanzar el vector
*
*/
void CountSort(vector<int> & vect, int max) {
unsigned n = vect.size();
vector<int> vect2(n);
vector<int> aux(max + 1);
for (unsigned i = 0; i < n; i++)
aux[vect[i]] = aux[vect[i]] + 1;
for (int i = 1; i < max; i++)
aux[i] += aux[i - 1];
for (int j = n; j >= 0; j--) {
vect2[aux[vect[j]] - 1] = vect[j];
aux[vect[j]] -= 1;
}
vect = vect2;
}
/**
*
* Aplicara el algoritmo LSDRadixSort sobre un vector
*
*
* @param vect Vector a ordenar
* @param max Numero maximo que puede alcanzar el vector
*
*/
void LSDRadixSortVector(vector<int> & vect, int max) {
vector<int> vacio(0);
int d;
unsigned n = vect.size();
vector<vector<int> > aux(n);
for (unsigned i = 0; i <= log10(max) + 1; i++) {
for (unsigned j = 0; j < 10; j++) {
aux[j] = vacio;
}
for (unsigned j = 0; j < n; j++) {
d = Digito(i, vect[j]);
aux[d].push_back(vect[j]);
}
for (unsigned j = 1; j < n; j++)
ConcatenarVector(aux[0], aux[j]);
vect = aux[0];
}
}
/**
*
* Concatenacion de 2 vectores
*
*
* @param v1 Vector inicial
* @param v2 Vector a insertar tras v1
*
*/
void ConcatenarVector(vector<int> & v1, const vector<int> & v2) {
for (unsigned i = 0; i < v2.size(); i++)
v1.push_back(v2[i]);
}
/**
*
* Obtiene un digito de un entero
*
*
* @param posicion Indice de posicion que obtener el digito del numero.
* @param num Numero del cual obtener el digito.
*
*/
int Digito(int posicion, int num) {
int aux = pow(10, posicion);
aux = num / aux;
aux = aux % 10;
return aux;
}
/**
*
* Generara una lista de enteros aleatorios
*
* @param lista Lista de enteros con tamano asignado
* @param lowlim Limite inferior aleatorio
* @param uplim Limite superior aleatorio
* @return True if lowlim <= uplim False if lowlim>uplim
*/
bool RandomList(list<int> & lista, int lowlim, int uplim, mt19937 aleatorio) {
int num;
if (lowlim <= uplim) {
auto iter = lista.begin();
while (iter != lista.end()) {
num = aleatorio() % (uplim - lowlim + 1) + lowlim;
*iter = num;
std::advance(iter, 1);
}
return true;
} else
return false;
}
/**
*
* Aplicara el algoritmo LSDRadixSort sobre una lista
*
*
* @param lista Lista a ordenar
* @param max Numero maximo que puede alcanzar la lista
*
*/
void LSDRadixSortLista(list<int> & lista, int max) {
list<int> vacio(0);
int d;
unsigned n = lista.size();
vector<list<int> > aux(n);
for (unsigned i = 0; i <= log10(max) + 1; i++) {
for (unsigned j = 0; j < 10; j++) {
aux[j] = vacio;
}
auto iter = lista.begin();
while (iter != lista.end()) {
d = Digito(i, *iter);
aux[d].push_back(*iter);
std::advance(iter, 1);
}
for (unsigned j = 1; j < n; j++)
aux[0].insert(aux[0].end(), aux[j].begin(), aux[j].end());
lista = aux[0];
}
}