-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.c
More file actions
423 lines (352 loc) · 14.8 KB
/
Copy pathMain.c
File metadata and controls
423 lines (352 loc) · 14.8 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#define MAX 100000 //Mudar para tamanho da instancia desejada
float comparacoesbubble=0;
float trocasbubble=0;
float comparacoesinsertion=0;
float trocasinsertion=0;
float comparacoesselection=0;
float trocasselection=0;
float comparacoesmerge=0;
float trocasmerge=0;
float comparacoesheap=0;
float trocasheap=0;
float comparacoesquick=0;
float trocasquick=0;
int tirarMedia(int vetor[], int tam){
int media=0;
int i=0;
for(i=0; i<tam; i++) {
media += vetor[i];
}
media = media/tam;
return media;
}
float tirarMediaF(float vetor[], int tam){
float media=0.0;
int i=0;
for(i=0; i < tam; i++) {
media += vetor[i];
}
media = media/tam;
return media;
}
void ExibirVetor(int vetor[], int tam){
int i=0;
while(i < tam) {
printf("|%d|, ", vetor[i]);
i++;
}
}
void GerarNumeros(int vetor[], int tam){
int i=0;
srand(time(NULL));
while(i < tam) {
vetor[i] = rand();
i++;
}
}
void bubbleSort(int vetor[], int tam){
int i,j;
int iAux;
for(i=1; i<tam; i++) {
for(j=tam-1; j>=i; j--) {
if(vetor[j-1] > vetor[j]) {
iAux = vetor[j-1];
vetor[j-1] = vetor[j];
vetor[j] = iAux;
trocasbubble++;
}
comparacoesbubble++;
}
}
return;
}
void selectionSort(int vetor[], int tam){
int i, j, min, aux;
for (i = 0; i < (tam-1); i++) {
min = i;
for (j = (i+1); j < tam; j++) {
if(vetor[j] < vetor[min]) min = j;
}
if (i != min) {
aux = vetor[i];
vetor[i] = vetor[min];
vetor[min] = aux;
trocasselection++;
}
comparacoesselection++;
}
}
int * insertionSort(int vetor[], int tam){
int i, j, atual;
for (i = 1; i < tam; i++) {
atual = vetor[i];
j = i - 1;
while ((j >= 0) && (atual < vetor[j])) {
vetor[j + 1] = vetor[j];
j = j - 1;
trocasinsertion++;
}
comparacoesinsertion++;
vetor[j + 1] = atual;
}
return (int *) vetor;
}
void heapSort(int vetor[], int tam) {
int i = tam/2, pai, filho, t;
for (;; ) {
if (i > 0) {
i--;
t = vetor[i];
}else {
tam--;
if (tam == 0) return;
t = vetor[tam];
vetor[tam] = vetor[0];
trocasheap++;
}
pai = i;
filho = i * 2 + 1;
while(filho < tam) {
comparacoesheap++;
if ((filho + 1 < tam) && (vetor[filho + 1] > vetor[filho]))
filho++;
if (vetor[filho] > t) {
vetor[pai] = vetor[filho];
pai = filho;
filho = pai * 2 + 1;
trocasheap++;
}else break;
}
vetor[pai] = t;
}
}
void mergeSort(int *vetor, int posicaoInicio, int posicaoFim) {
int i, j, k, metadeTamanho, *vetorTemp;
if(posicaoInicio == posicaoFim) return;
// ordenacao recursiva das duas metades
metadeTamanho = (posicaoInicio + posicaoFim ) / 2;
mergeSort(vetor, posicaoInicio, metadeTamanho);
mergeSort(vetor, metadeTamanho + 1, posicaoFim);
// intercalacao no vetor temporario t
i = posicaoInicio;
j = metadeTamanho + 1;
k = 0;
vetorTemp = (int *) malloc(sizeof(int) * (posicaoFim - posicaoInicio + 1));
while(i < metadeTamanho + 1 || j < posicaoFim + 1) {
if (i == metadeTamanho + 1 ) { // i passou do final da primeira metade, pegar v[j]
vetorTemp[k] = vetor[j];
j++;
k++;
trocasmerge++;
}
else {
if (j == posicaoFim + 1) { // j passou do final da segunda metade, pegar v[i]
vetorTemp[k] = vetor[i];
i++;
k++;
}
else {
if (vetor[i] < vetor[j]) {
vetorTemp[k] = vetor[i];
i++;
k++;
}
else {
vetorTemp[k] = vetor[j];
j++;
k++;
}
}
trocasmerge++;
}
comparacoesmerge++;
}
// copia vetor intercalado para o vetor original
for(i = posicaoInicio; i <= posicaoFim; i++) {
vetor[i] = vetorTemp[i - posicaoInicio];
}
free(vetorTemp);
}
int Separa(int *v, int inicio, int fim) {
int pivo = v[fim], i, j = inicio, valor;
for(i=inicio; i<fim; i++) {
if(v[i] < pivo) {
valor = v[i];
v[i] = v[j];
v[j] = valor;
j++;
trocasquick++;
}
comparacoesquick++;
}
if(pivo <= v[j]) {
v[fim] = v[j];
v[j] = pivo;
}
return j;
}
void quickSort(int *vetor, int inicio, int fim) {
if(inicio >= fim)
return;
int pivoIndex = Separa(vetor, inicio, fim);
quickSort(vetor, inicio, pivoIndex-1);
quickSort(vetor, pivoIndex+1, fim);
}
void ClonaVetor(int *vetor, int *vetor2){
for (int i = 0; i < MAX; i++) {
vetor2[i] = vetor[i];
}
}
int main(){
int vetor[MAX];
int vetorbubble[MAX];
int vetorinsertion[MAX];
int vetorselection[MAX];
int vetorheap[MAX];
int vetormerge[MAX];
int vetorquick[MAX];
float vTempobubble[10], vComparacoesbubble[10], vTrocasbubble[10];
float vTempoinsertion[10], vComparacoesinsertion[10], vTrocasinsertion[10];
float vTemposelection[10], vComparacoesselection[10], vTrocasselection[10];
float vTempomerge[10], vComparacoesmerge[10], vTrocasmerge[10];
float vTempoheap[10], vComparacoesheap[10], vTrocasheap[10];
float vTempoquick[10], vComparacoesquick[10], vTrocasquick[10];
struct timeval antesbubble, depoisbubble;
struct timeval antesinsertion, depoisinsertion;
struct timeval antesselection, depoisselection;
struct timeval antesmerge, depoismerge;
struct timeval antesheap, depoisheap;
struct timeval antesquick, depoisquick;
float deltabubble, tempoMediobubble;
float deltainsertion, tempoMedioinsertion;
float deltaselection, tempoMedioselection;
float deltamerge, tempoMediomerge;
float deltaheap, tempoMedioheap;
float deltaquick, tempoMedioquick;
float comparacoesMediabubble, trocasMediabubble;
float comparacoesMediainsertion, trocasMediainsertion;
float comparacoesMediaselection, trocasMediaselection;
float comparacoesMediamerge, trocasMediamerge;
float comparacoesMediaheap, trocasMediaheap;
float comparacoesMediaquick, trocasMediaquick;
int i=0;
for(i=0; i < 10; i++) {
GerarNumeros(vetor, MAX);
ClonaVetor(vetor, vetorbubble);
ClonaVetor(vetor, vetorinsertion);
ClonaVetor(vetor, vetorselection);
ClonaVetor(vetor, vetormerge);
ClonaVetor(vetor, vetorheap);
ClonaVetor(vetor, vetorquick);
/////////////////////////////bubbleSort
gettimeofday(&antesbubble, 0);
bubbleSort(vetor, MAX);
gettimeofday(&depoisbubble, 0);
deltabubble = (depoisbubble.tv_sec + depoisbubble.tv_usec/1000000.0) - (antesbubble.tv_sec + antesbubble.tv_usec /1000000.0);
vComparacoesbubble[i] = comparacoesbubble;
vTrocasbubble[i] = trocasbubble;
vTempobubble[i] = deltabubble;
comparacoesbubble=0;
trocasbubble=0;
/////////////////////////////////////////////////insertionSort
gettimeofday(&antesinsertion, 0);
insertionSort(vetorinsertion, MAX);
gettimeofday(&depoisinsertion, 0);
deltainsertion = (depoisinsertion.tv_sec + depoisinsertion.tv_usec/1000000.0) - (antesinsertion.tv_sec + antesinsertion.tv_usec /1000000.0);
vComparacoesinsertion[i] = comparacoesinsertion;
vTrocasinsertion[i] = trocasinsertion;
vTempoinsertion[i] = deltainsertion;
comparacoesinsertion=0;
trocasinsertion=0;
/////////////////////////////////////////////////selectionSort
gettimeofday(&antesselection, 0);
selectionSort(vetorselection, MAX);
gettimeofday(&depoisselection, 0);
deltaselection = (depoisselection.tv_sec + depoisselection.tv_usec/1000000.0) - (antesselection.tv_sec + antesselection.tv_usec /1000000.0);
vComparacoesselection[i] = comparacoesselection;
vTrocasselection[i] = trocasselection;
vTemposelection[i] = deltaselection;
comparacoesselection=0;
trocasselection=0;
/////////////////////////////////////////////////mergeSort
gettimeofday(&antesmerge, 0);
mergeSort(vetormerge, 0, MAX);
gettimeofday(&depoismerge, 0);
deltamerge = (depoismerge.tv_sec + depoismerge.tv_usec/1000000.0) - (antesmerge.tv_sec + antesmerge.tv_usec /1000000.0);
vComparacoesmerge[i] = comparacoesmerge;
vTrocasmerge[i] = trocasmerge;
vTempomerge[i] = deltamerge;
comparacoesmerge=0;
trocasmerge=0;
/////////////////////////////////////////////////heapSort
gettimeofday(&antesheap, 0);
heapSort(vetor, MAX);
gettimeofday(&depoisheap, 0);
deltaheap = (depoisheap.tv_sec + depoisheap.tv_usec/1000000.0) - (antesheap.tv_sec + antesheap.tv_usec /1000000.0);
vComparacoesheap[i] = comparacoesheap;
vTrocasheap[i] = trocasheap;
vTempoheap[i] = deltaheap;
comparacoesheap=0;
trocasheap=0;
/////////////////////////////////////////////////quickSort
gettimeofday(&antesquick, 0);
quickSort(vetorquick,0, MAX);
gettimeofday(&depoisquick, 0);
deltaquick= (depoisquick.tv_sec + depoisquick.tv_usec/1000000.0) - (antesquick.tv_sec + antesquick.tv_usec /1000000.0);
vComparacoesquick[i] = comparacoesquick;
vTrocasquick[i] = trocasquick;
vTempoquick[i] = deltaquick;
comparacoesquick=0;
trocasquick=0;
///////////////////
}
comparacoesMediabubble = tirarMediaF(vComparacoesbubble,10);
trocasMediabubble = tirarMediaF(vTrocasbubble,10);
tempoMediobubble = tirarMediaF(vTempobubble,10);
comparacoesMediainsertion = tirarMediaF(vComparacoesinsertion,10);
trocasMediainsertion = tirarMediaF(vTrocasinsertion,10);
tempoMedioinsertion = tirarMediaF(vTempoinsertion,10);
comparacoesMediaselection = tirarMediaF(vComparacoesselection,10);
trocasMediaselection = tirarMediaF(vTrocasselection,10);
tempoMedioselection = tirarMediaF(vTemposelection,10);
comparacoesMediamerge = tirarMediaF(vComparacoesmerge,10);
trocasMediamerge = tirarMediaF(vTrocasmerge,10);
tempoMediomerge = tirarMediaF(vTempomerge,10);
comparacoesMediaheap = tirarMediaF(vComparacoesheap,10);
trocasMediaheap = tirarMediaF(vTrocasheap,10);
tempoMedioheap = tirarMediaF(vTempoheap,10);
comparacoesMediaquick = tirarMediaF(vComparacoesquick,10);
trocasMediaquick = tirarMediaF(vTrocasquick,10);
tempoMedioquick = tirarMediaF(vTempoquick,10);
printf("\n////////////////////////////////////////\n");
printf("\n\n\nTempo do bubbleSort: %f", tempoMediobubble);
printf("\nComparacoes do bubbleSort: %f", comparacoesMediabubble);
printf("\nTrocas do bubbleSort: %f", trocasMediabubble);
printf("\n////////////////////////////////////////\n");
printf("\n\n\nTempo do insertionSort: %f", tempoMedioinsertion);
printf("\nComparacoes do insertionSort: %f", comparacoesMediainsertion);
printf("\nTrocas do insertionSort: %f", trocasMediainsertion);
printf("\n////////////////////////////////////////\n");
printf("\n\n\nTempo do selectionSort: %f", tempoMedioselection);
printf("\nComparacoes do selectionSort: %f", comparacoesMediaselection);
printf("\nTrocas do selectionSort: %f", trocasMediaselection);
printf("\n////////////////////////////////////////\n");
printf("\n\n\nTempo do mergeSort: %f", tempoMediomerge);
printf("\nComparacoes do mergeSort: %f", comparacoesMediamerge);
printf("\nTrocas do mergeSort: %f", trocasMediamerge);
printf("\n////////////////////////////////////////\n");
printf("\n\n\nTempo do heapSort: %f", tempoMedioheap);
printf("\nComparacoes do heapSort: %f", comparacoesMediaheap);
printf("\nTrocas do heapSort: %f", trocasMediaheap);
printf("\n////////////////////////////////////////\n");
printf("\n\n\nTempo do quickSort: %f", tempoMedioquick);
printf("\nComparacoes do quickSort : %f", comparacoesMediaquick);
printf("\nTrocas do quickSort : %f", trocasMediaquick);
system("pause");
return 0;
}