-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance_algorithms.c
628 lines (517 loc) · 17.7 KB
/
performance_algorithms.c
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
BIBLIOTECAS USADAS
*/
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <locale.h>
/*
ENCONTRA O SISTEMA OPERACIONAL USADO
UNIX = Linux/Mac
WIN = Windowns
*/
#ifdef __unix__
#include <unistd.h>
#include <stdlib.h>
#elif defined(_WIN32) || defined(WIN32)
#define OS_Windows
#include <windows.h>
#endif
/*
ESTRUTURA USADA PARA GUARDAR OS DADOS DE TESTE
*/
typedef struct {
char name[100];
double unsorted;
double sorted;
} Algorithm;
/*
FUNÇÕES USADAS
*/
// Escolhe as opções de execução
void ChooseOptions(int title, Algorithm *algorithms);
// Faz a seleção do tamanho do vetor
int ChooseSize();
// Aloca memória para os vetores usados
int *AllocateMemory(int n);
// Preenche os vetores com valores aleatórios
void FillVector(int *vet, int n);
// Faz a limpeza do terminal de acordo com o sistema operacional
void CleanConsole();
// Printa relatório de desempenho
void print_report(Algorithm *algorithms);
// Funções de Ordenação
void BubbleSort(int *vetor, int n);
void SelectionSort(int *vetor, int n);
void InsertionSort(int *vetor, int n);
void HeapSort(int *vet, int N);
void QuickSort(int *vet, int start, int end);
void RadixSort(int *vetor, int tamanho);
void MergeSort(int *V, int inicio, int fim);
// Auxilia HeapSort
void criaHeap(int *vet, int i, int f);
// Auxilia o MergeSort
void merge(int *V, int inicio, int meio, int fim);
/*
FUNÇÃO PRINCIPAL DE DEFINIÇÕES
*/
int main(){
/*
LIMPANDO CONSOLE E OBTENDO
VARIÁVEIS IMPORTANTES
*/
setlocale(LC_ALL, "Portuguese");
CleanConsole();
int title = 1;
Algorithm algorithms[7];
ChooseOptions(title, &algorithms);
return 0;
}
/*
FUNÇÕES AUXILIARES
*/
void CleanConsole(){
#ifdef OS_Windows
/* Codigo Windows */
system("cls");
#else
/* Codigo GNU/Linux */
system("clear");
#endif
}
void criaHeap(int *vet, int i, int f){
int aux = vet[i];
int j = i * 2 + 1;
while (j <= f){
if(j < f){
if(vet[j] < vet[j + 1]){
j = j + 1;
}
}
if(aux < vet[j]){
vet[i] = vet[j];
i = j;
j = 2 * i + 1;
}else{
j = f + 1;
}
}
vet[i] = aux;
}
void merge(int *V, int inicio, int meio, int fim){
int *temp, p1, p2, tamanho, i, j, k;
int fim1 = 0, fim2 = 0;
tamanho = fim-inicio+1;
p1 = inicio;
p2 = meio+1;
temp = (int *) malloc(tamanho*sizeof(int));
if(temp != NULL){
for(i=0; i<tamanho; i++){
if(!fim1 && !fim2){
if(V[p1] < V[p2])
temp[i]=V[p1++];
else
temp[i]=V[p2++];
if(p1>meio) fim1=1;
if(p2>fim) fim2=1;
}else{
if(!fim1)
temp[i]=V[p1++];
else
temp[i]=V[p2++];
}
}
for(j=0, k=inicio; j<tamanho; j++, k++)
V[k]=temp[j];
}
free(temp);
}
int *AllocateMemory(int n){
printf("\n\nAlocando memória, aguarde um momento...");
sleep(2);
int *vet = (int *) malloc(n * sizeof(int));
if (vet == NULL){
CleanConsole();
printf("Erro na alocação de memória");
exit(1);
}
CleanConsole();
return vet;
}
void FillVector(int *vet, int n){
printf("\n\nPreenchendo vetor, aguarde um momento...");
sleep(2);
int i;
for (i = 0; i < n; i++){
vet[i] = rand() % n;
}
CleanConsole();
}
void print_report(Algorithm *algorithms){
int i;
printf("\n\n==================================");
printf("\nRelatorio de Desempenho");
printf("\n==================================");
for (i = 0; i < 7; i++){
printf("\nAlgoritmo %s", algorithms[i].name);
printf("\nUnsorted time %f", algorithms[i].unsorted);
printf("\nSorted time %f", algorithms[i].sorted);
printf("\n==============================");
}
}
/*
FUNÇÕES DE ORDENAÇÃO
*/
void MergeSort(int *V, int inicio, int fim){
int meio;
if(inicio < fim){
meio = floor((inicio+fim)/2);
MergeSort(V,inicio,meio);
MergeSort(V,meio+1,fim);
merge(V,inicio,meio,fim);
}
}
void RadixSort(int *vetor, int tamanho) {
int i;
int *b;
int maior = vetor[0];
int exp = 1;
b = (int *)calloc(tamanho, sizeof(int));
for (i = 0; i < tamanho; i++) {
if (vetor[i] > maior)
maior = vetor[i];
}
while (maior/exp > 0) {
int bucket[10] = { 0 };
for (i = 0; i < tamanho; i++)
bucket[(vetor[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
bucket[i] += bucket[i - 1];
for (i = tamanho - 1; i >= 0; i--)
b[--bucket[(vetor[i] / exp) % 10]] = vetor[i];
for (i = 0; i < tamanho; i++)
vetor[i] = b[i];
exp *= 10;
}
free(b);
}
void QuickSort(int *vet, int start, int end){
int pivo, aux, i, j, m, a;
i = start;
j = end;
m = ((i + j) / 2); //definindo o meio do vetor
pivo = vet[m]; //valor no meio do vetor sendo atribuido como pivo
do{
//comparação dos valores da esquerda com o pivo:
while (vet[i] < pivo) //enquanto o valor da esquerda for menor que o pivo avance uma posição
i = i + 1;
//comparação dos valores da direita com o pivo
while (vet[j] > pivo) //enquanto o valor da direita for maior que o pivo recue uma posição
j = j - 1;
//trocar de posição (valor maior a direita com valor menor da esquerda)
if(i <= j){
aux = vet[i];
vet[i] = vet[j];
vet[j] = aux;
i = i + 1;
j = j - 1;
}
}while(j > i); //o lopping continua enquanto o valor de j for maior que o de i
if(start < j) QuickSort(vet, start, j);
if(i < end) QuickSort(vet, i, end);
}
void HeapSort(int *vet, int N) {
int i, aux;
for(i=(N-1)/2; i >= 0; i--){
criaHeap(vet, i, N-1);
}
for (i = N-1; i >= 1; i--){
aux = vet[0];
vet[0] = vet[i];
vet[i] = aux;
criaHeap(vet, 0, i - 1);
}
}
void InsertionSort(int *vetor, int n){
int i, j, aux;
for (i = 1; i < n; i++) {
aux = vetor[i];
for (j = i; (j > 0) && (aux < vetor[j-1]); j--){
vetor[j] = vetor[j-1];
}
vetor[j] = aux;
}
}
void SelectionSort(int *vetor, int n){
int i, j, menor, troca;
for (i = 0; i < n - 1; i++) {
menor = i;
for (j = i + 1; j < n; j++){
if (vetor[j] < vetor[menor]) {
menor = j;
}
}
if (i != menor) {
troca = vetor[i];
vetor[i] = vetor[menor];
vetor[menor] = troca;
}
}
}
void BubbleSort(int *vetor, int n){
int i, continua, aux;
int fim = n;
// Ordenação por bolha
do {
continua = 0;
for (i = 0; i < fim-1; i++) {
if (vetor[i] > vetor[i+1]){
aux = vetor[i];
vetor[i] = vetor[i+1];
vetor[i+1] = aux;
continua = i;
}
}
fim --;
} while (continua != 0 );
}
/*
FUNÇÕES DE ESCOLHA E MEDIÇÃO
*/
int ChooseSize(){
//CleanConsole();
printf("\n\n==================================");
printf("\nANALISE DE ALGORITMOS DE ORDENACAO");
printf("\n==================================");
printf("\nEscolha o tamanho do vetor");
printf("\n 1 - 1.000");
printf("\n 2 - 5.000");
printf("\n 3 - 10.000");
printf("\n 4 - 20.000");
printf("\n 5 - 50.000");
printf("\n 6 - 100.000\n");
int option;
printf("\n\nQual tamanho deseja usar?\n");
scanf("%d", &option);
switch(option) {
case 1:
printf("\n\nEscolhido 1000 elementos ");
return 1000;
break;
case 2:
printf("\n\nEscolhido 5000 elementos");
return 5000;
break;
case 3:
printf("\n\nEscolhido 10000 elementos");
return 10000;
break;
case 4:
printf("\n\nEscolhido 20000 elementos");
return 20000;
break;
case 5:
printf("\n\nEscolhido 50000 elementos");
return 50000;
break;
case 6:
printf("\n\nEscolhido 100000 elementos");
return 100000;
break;
default :
printf("\nOpcao invalida.");
sleep(5);
ChooseSize();
break;
}
exit(1);
}
void ChooseOptions(int title, Algorithm *algorithms){
int *vet, size, option;
struct timeval Tempo_antes, Tempo_depois;
double deltaT;
if (title){
printf("\n\n==================================");
printf("\nANALISE DE ALGORITMOS DE ORDENACAO");
printf("\n==================================");
printf("\n 1 - BubbleSort");
printf("\n 2 - InsertionSort");
printf("\n 3 - SelectionSort");
printf("\n 4 - RadixSort");
printf("\n 5 - MergeSort");
printf("\n 6 - QuickSort");
printf("\n 7 - HeapSort\n");
printf("\n 8 - Relatorio de Desempenho\n");
}
printf("\n\nQual algoritmo deseja executar?\n");
scanf("%d", &option);
switch(option) {
case 1:
printf("\n\nOpcao 1");
algorithms[0].name[30] = 'Teste';
size = ChooseSize();
vet = AllocateMemory(size);
FillVector(vet, size);
// Teste desordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
BubbleSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
algorithms[0].unsorted = deltaT;
printf("\nTempo para execucao do algoritmo desordenado: %f segundos\n", algorithms[0].unsorted);
// Teste ordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
BubbleSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
algorithms[0].sorted = deltaT;
printf("Tempo para execucao do algoritmo ordenado: %f segundos\n", algorithms[0].sorted);
free(vet);
ChooseOptions(1, algorithms);
break;
case 2:
printf("\n\nOpcao 2");
size = ChooseSize();
vet = AllocateMemory(size);
FillVector(vet, size);
// Teste desordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
InsertionSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("\nTempo para execucao do algoritmo desordenado: %f segundos\n", deltaT);
algorithms[1].unsorted = deltaT;
// Teste ordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
InsertionSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("Tempo para execucao do algoritmo ordenado: %f segundos\n", deltaT);
algorithms[1].sorted = deltaT;
free(vet);
ChooseOptions(1, algorithms);
break;
case 3:
printf("\n\nOpcao 3");
size = ChooseSize();
vet = AllocateMemory(size);
FillVector(vet, size);
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
SelectionSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("\nTempo para execucao do algoritmo desordenado: %f segundos\n", deltaT);
algorithms[2].unsorted = deltaT;
// Teste ordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
SelectionSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("Tempo para execucao do algoritmo ordenado: %f segundos\n", deltaT);
algorithms[2].sorted = deltaT;
free(vet);
ChooseOptions(1, algorithms);
break;
case 4:
printf("\n\nOpcao 4");
size = ChooseSize();
vet = AllocateMemory(size);
FillVector(vet, size);
// Teste desordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
RadixSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("\nTempo para execucao do algoritmo desordenado: %f segundos\n", deltaT);
algorithms[3].unsorted = deltaT;
// Teste ordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
RadixSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("Tempo para execucao do algoritmo ordenado: %f segundos\n", deltaT);
algorithms[3].sorted = deltaT;
free(vet);
ChooseOptions(1, algorithms);
break;
case 5:
printf("\n\nOpcao 5");
size = ChooseSize();
vet = AllocateMemory(size);
FillVector(vet, size);
// Teste desordenado
printf("\nIniciando teste de desempenho em");
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
MergeSort(vet, 0, size-1);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("\nTempo para execucao do algoritmo desordenado: %f segundos\n", deltaT);
algorithms[4].unsorted = deltaT;
// Teste ordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
MergeSort(vet, 0, size-1);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("Tempo para execucao do algoritmo ordenado: %f segundos\n", deltaT);
algorithms[4].sorted = deltaT;
free(vet);
ChooseOptions(1, algorithms);
break;
case 6:
printf("\n\nOpcao 6");
size = ChooseSize();
vet = AllocateMemory(size);
FillVector(vet, size);
// Teste desordenado
printf("\nIniciando teste de desempenho em");
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
QuickSort(vet, 0, size-1);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("\nTempo para execucao do algoritmo desordenado: %f segundos\n", deltaT);
algorithms[5].unsorted = deltaT;
// Teste ordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
QuickSort(vet, 0, size-1);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("Tempo para execucao do algoritmo ordenado: %f segundos\n", deltaT);
algorithms[5].sorted = deltaT;
free(vet);
ChooseOptions(1, algorithms);
break;
case 7:
printf("\n\nOpcao 7");
vet = ChooseSize();
vet = AllocateMemory(size);
FillVector(vet, size);
// Teste desordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
HeapSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("\nTempo para execucao do algoritmo desordenado: %f segundos\n", deltaT);
algorithms[6].unsorted = deltaT;
// Teste ordenado
gettimeofday(&Tempo_antes, NULL); // Tempo Inicial
HeapSort(vet, size);
gettimeofday(&Tempo_depois, NULL); // Tempo final
deltaT = (Tempo_depois.tv_sec + Tempo_depois.tv_usec/1000000.0) - (Tempo_antes.tv_sec + Tempo_antes.tv_usec/1000000.0);
printf("Tempo para execucao do algoritmo ordenado: %f segundos\n", deltaT);
algorithms[6].sorted = deltaT;
free(vet);
ChooseOptions(1, algorithms);
break;
case 8:
CleanConsole();
printf("\nMontando o relatorio, aguarde um momento...");
sleep(5);
return print_report(algorithms);
default:
printf("\nOpcao invalida.");
sleep(5);
ChooseOptions(1, algorithms);
break;
}
exit(1);
}