-
Notifications
You must be signed in to change notification settings - Fork 4
/
ordenacao.c
75 lines (62 loc) · 1.57 KB
/
ordenacao.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "utils.h"
#include "ordenacao.h"
void print_help() {
puts("-s selecion sort");
puts("-i insertion sort");
puts("-b bubble sort");
puts("-m merge sort");
puts("-q quick sort");
puts("-h ajuda");
}
int main(int argc, char const *argv[]) {
Bool sucesso = TRUE;
int tipo, size, i;
clock_t start, end;
double elapsed;
int* array;
void (*sort[])(int*, int) = {selection_sort,
insertion_sort,
bubble_sort,
merge_sort,
quicksort};
if(argc < 2) {
puts("Uso: ordecao <opções>");
return 1;
}
if(argv[1][0] == '-') {
switch (argv[1][1]) {
case 's': tipo = 0; break;
case 'i': tipo = 1; break;
case 'b': tipo = 2; break;
case 'm': tipo = 3; break;
case 'q': tipo = 4; break;
case 'h':
default:
print_help();
return 0;
}
}
scanf("%i", &size);
array = (int*) malloc(sizeof(int)*size);
for(i = 0; i < size; i++)
scanf("%i", &array[i]);
start = clock();
//selection_sort(array, size);
(*sort[tipo])(array, size);
end = clock();
elapsed = ((double)(end - start)) / CLOCKS_PER_SEC;
for(i = 1; i < size; i++) {
// printf("%i\n", array[i]);
if(array[i-1] > array[i])
sucesso = FALSE;
}
if(sucesso)
printf("Arranjo com %i elementos ordenado com sucesso\n", size);
else
puts("Algo deu errado");
printf("Tempo de processamento: %.2lf segundos.\n", elapsed);
return 0;
}