diff --git a/Makefile b/Makefile index c9a7d28ed67b66b953dfc5f5518357527bd31909..102f90b7790b7ced0708df15b0c97010ed2801da 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ Quadra.exe: libbusca.a gcc Quadra.c -I./ -L./ -lbusca -o Quadra.exe libbusca.a: busca.o - ar -cru libbusca.a busca.o + ar -cru libbusca.a busca.o busca.o: gcc -c busca.c -o busca.o diff --git a/Quadra.c b/Quadra.c index ca87919335fbda6e451fda95f81bcbb2e930a37a..221ee1511c8e49b18dc9bc259110a6fb9233838f 100644 --- a/Quadra.c +++ b/Quadra.c @@ -1,48 +1,36 @@ #include "busca.h" #include <time.h> -void vetSort(int *vet[], int Tam, int max) +void vetSort(int *vet, int *vet_ord, int Tam, int max) { int i; srand(time(NULL)); - *vet = (int *) malloc ((Tam+1) * sizeof(int)); for (i = 1; i <= Tam; i++) { - (*vet)[i] = 1 + (rand() % max); - printf("Valor %d: %d\n", i, (*vet)[i]); + vet[i] = 1 + (rand() % max); + vet_ord[i] = vet[i]; } } -void vetcpy(int vet[], int *vet_ord[], int Tam) -{ - int i; - *vet_ord = (int *) malloc ((Tam + 1) * sizeof(int)); - for (i = 1; i <= Tam; i++) - (*vet_ord[i]) = vet[i]; -} - void imprime (int *vet, int N) { int i; - printf("Entrou\nTamanho %d\n", N); - for (i = 1; i <= 5; i++) - printf("%d ", i); - /*{ - printf("Interacao %d: ", i); + for (i = 1; i <= N; i++) + { if ((i % 10) == 0) printf ("\n\n"); printf ("%d ", vet[i]); - }*/ + } } -void jogar (int *vet[], int *vet_ord[], int N) +void jogar (int *vet, int *vet_ord, int N) { int x, i = 0, j; printf ("Entre com a Quadra de valores: "); for (j = 0; j < 4; j++) { scanf ("%d", &x); - if ((pesqseq(*vet, x, N) != 0) || (pesqbin(*vet_ord, x, N) != 0)) + if ((pesqseq(vet, x, N) != 0) || (pesqbin(vet_ord, x, N) != 0)) i++; } if (i == 4) @@ -53,39 +41,63 @@ void jogar (int *vet[], int *vet_ord[], int N) printf ("Nenhum valor encontrado!\n"); } +void vetcpy (int *v1, int *v2, int N) +{ + int i; + for (i = 1; i <= N; i++) + v2[i] = v1[i]; +} + +int tempoEmMili(clock_t inicio, clock_t fim) +{ + double tt; + tt = (((fim-inicio)*1000000)/CLOCKS_PER_SEC); + return tt; +} + void jornada(int Tam, int max) { - int i, *mat[10000], *mat_ord[10000], time[4]; //cada um dos 4 times conta o tempo de um dos algoritmos de ordenação + clock_t ti, tf; + int i, *mat[10000], *mat_ord[10000], tt; for (i = 0; i < 10000; i++) { - vetSort(mat[i], Tam, max); - vetcpy(mat[i], mat_ord[i], Tam); + mat[i] = (int *) malloc ((Tam+1)*sizeof(int)); + mat_ord[i] = (int *) malloc ((Tam+1)*sizeof(int)); + vetSort(mat[i], mat_ord[i], Tam, max); } - // inicia time[0] + ti = clock(); for (i = 0; i < 10000; i++) SelectSort(mat_ord[i], Tam); - // time[0] = tempoMaq - time[0] + tf = clock(); + tt = tempoEmMili(ti,tf); + printf("SelectSort: %d micro-segundos\n", tt); for (i = 0; i < 10000; i++) vetcpy(mat[i], mat_ord[i], Tam); - // inicia time [1] + ti = clock(); for (i = 0; i < 10000; i++) BubbleSort(mat_ord[i], Tam); - // time[1] = tempoMaq - time[1] + tf = clock(); + tt = tempoEmMili(ti,tf); + printf("BubbleSort: %d micro-segundos\n", tt); for (i = 0; i < 10000; i++) vetcpy(mat[i], mat_ord[i], Tam); - // inicia time [2] + ti = clock(); for (i = 0; i < 10000; i++) QuickSort_rec(mat_ord[i], 1, Tam); - // time[2] = tempoMaq - time[2] + tf = clock(); + tt = tempoEmMili(ti,tf); + printf("QuickSort Recursivo: %d micro-segundos\n", tt); for (i = 0; i < 10000; i++) vetcpy(mat[i], mat_ord[i], Tam); - // inicia time [3] + ti = clock(); for (i = 0; i < 10000; i++) QuickSort_it(mat_ord[i], Tam); - // time[3] = tempoMaq - time[3] + tf = clock(); + tt = tempoEmMili(ti,tf); + printf("QuickSort Iterativo: %d micro-segundos\n", tt); } int main (void) @@ -96,9 +108,9 @@ int main (void) scanf ("%d", &Tam); printf ("Entre com valor maximo dos numeros aleatorios: "); scanf ("%d", &max); - vetSort(&vet, Tam, max); - imprime(vet, Tam); - vetcpy(vet, &vet_ord, Tam); + vet = (int *) malloc ((Tam+1) * sizeof(int)); + vet_ord = (int *) malloc ((Tam+1) * sizeof(int)); + vetSort(vet, vet_ord, Tam, max); QuickSort_it(vet_ord, Tam); printf ("\nMenu:\n"); menu = 1; @@ -107,7 +119,7 @@ int main (void) switch (menu) { case 1: - printf ("1 - Mostrar menu\n2 - Imprimir Vetor de Numeros Aleatorios\n3 - Imprimir Vetor Ordenado\n4 - Jogar\n5 - Fazer jornada dos 10.000 vetores\n"); + printf ("1 - Mostrar menu\n2 - Imprimir Vetor de Numeros Aleatorios\n3 - Imprimir Vetor Ordenado\n4 - Jogar\n5 - Fazer jornada dos 10.000 vetores\n0 - Sair\n"); break; case 2: imprime (vet, Tam); @@ -116,7 +128,7 @@ int main (void) imprime (vet_ord, Tam); break; case 4: - jogar (&vet, &vet_ord, Tam); + jogar (vet, vet_ord, Tam); break; case 5: jornada(Tam, max); diff --git a/Quadra.exe b/Quadra.exe index faec9f4aeaf3876c09adeba29ac01cf7af5457d2..11978c9bf0d50dc6b84aa72069e8426b7de9c339 100755 Binary files a/Quadra.exe and b/Quadra.exe differ diff --git a/busca.c b/busca.c index fcce726928f3ce5755e0c8776f46df1debaa4f7a..ae8e2733ba4f9b3cfa156d9667294777d99a4563 100644 --- a/busca.c +++ b/busca.c @@ -66,7 +66,6 @@ int mediana (int *vet, int esq, int dir) { int med = (esq+dir)/2; int E = vet[esq], D = vet[dir], M = vet[med]; - printf("esq = %d, dir = %d, med = %d\n", E, D, M); if (E <= D) { if (E <= M) //descobrimos se esq é o menor elemento ou não @@ -95,10 +94,7 @@ int mediana (int *vet, int esq, int dir) void Particao (int *vet, int esq, int dir, int *pos_pivo) { - int i, j, aux, pivo, med; - printf("Entrou!\n"); - med = mediana(vet, esq, dir); - printf("Passou!\n"); + int i, j, aux, pivo, med = mediana(vet, esq, dir); pivo = vet[med]; vet[med] = vet[esq]; vet[esq] = pivo; @@ -120,7 +116,6 @@ void Particao (int *vet, int esq, int dir, int *pos_pivo) vet[esq] = vet[j]; vet[j] = pivo; *pos_pivo = j; - printf("passou!\n"); } void QuickSort_rec (int *vet, int esq, int dir) diff --git a/busca.o b/busca.o index c9f8eff56f78c41bdbecfd7208e2e67fd41ba704..90ac0aa251a1e0fa3d616af1f9dfda3f0ae11b83 100644 Binary files a/busca.o and b/busca.o differ diff --git a/libbusca.a b/libbusca.a index f106f2032029f25f16e9562823810d436716395f..a2553d89037bc25e1342e7ee2d1510f735bba2e6 100644 Binary files a/libbusca.a and b/libbusca.a differ diff --git a/programa b/programa new file mode 100755 index 0000000000000000000000000000000000000000..460c7136ee3877ef8c08bd1b76d68f7cc772112e Binary files /dev/null and b/programa differ