Skip to content
Snippets Groups Projects
Commit 820c6fe8 authored by Fernando Erd's avatar Fernando Erd :ok_hand:
Browse files

Arrumando Pesquisa Sequencial

parent e32ac312
No related branches found
No related tags found
No related merge requests found
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/time.h> #include <sys/time.h>
#define TAM 15 //tamanho do vetor #define TAM 100 //tamanho do vetor
#define NUMMAX 50 //constante que tem o máximo de valores #define NUMMAX 50 //constante que tem o máximo de valores
No preview for this file type
#include "Estruturas.h" #include "Estruturas.h"
#include "Funções.h" #include "Funções.h"
void TempoSelectSort (int *Vetor) {
struct timeval inicio, final;
int tmili, i;
gettimeofday(&inicio, NULL);
for (i = 0; i < 10000; i ++) {
GeraVetor(Vetor);
SelectSort (Vetor);
}
gettimeofday(&final, NULL);
tmili = (int) (1000 * (final.tv_sec - inicio.tv_sec) + (final.tv_usec - inicio.tv_usec) / 1000);
printf ("O SelectSort Demorou %d Centesimos\n", tmili);
}
void TempoBubbleSort (int *Vetor) {
struct timeval inicio, final;
int tmili, i;
gettimeofday(&inicio, NULL);
for (i = 0; i < 10000; i ++) {
GeraVetor(Vetor);
BubbleSort (Vetor);
}
gettimeofday(&final, NULL);
tmili = (int) (1000 * (final.tv_sec - inicio.tv_sec) + (final.tv_usec - inicio.tv_usec) / 1000);
printf ("O BubbleSort Demorou %d Centesimos\n", tmili);
}
int main () { int main () {
int Vetor[TAM], Escolhidos[4]; int Vetor[TAM], Escolhidos[4];
struct timeval inicio, final; int comando;
int tmili, i, comando;
printf ("1 Para Jogar e 2 Para Comparar Algoritmos\n"); printf ("1 Para Jogar e 2 Para Comparar Algoritmos\n");
scanf ("%d", &comando); scanf ("%d", &comando);
...@@ -17,13 +44,7 @@ int main () { ...@@ -17,13 +44,7 @@ int main () {
ImprimeVetor (Vetor); ImprimeVetor (Vetor);
break; break;
case 2: case 2:
gettimeofday(&inicio, NULL); TempoSelectSort (Vetor);
for (i = 0; i < 10000; i ++) { TempoBubbleSort (Vetor);
GeraVetor(Vetor);
SelectSort (Vetor);
}
gettimeofday(&final, NULL);
tmili = (int) (1000 * (final.tv_sec - inicio.tv_sec) + (final.tv_usec - inicio.tv_usec) / 1000);
printf ("O SelectSort Demorou %d Centesimos\n", tmili);
} }
} }
...@@ -23,10 +23,11 @@ int PesquisaBinaria (int *Vetor, int Elem) { ...@@ -23,10 +23,11 @@ int PesquisaBinaria (int *Vetor, int Elem) {
int PesquisaSequencial(int *Vetor, int Elem) { int PesquisaSequencial(int *Vetor, int Elem) {
int i; int i;
Vetor[0] = Elem; Vetor[0] = Elem;
for (i=TAM-1; i>=0; i++) { for (i = 0; i < TAM; i++) {
if (Vetor[i] == Elem) if (Vetor[i] == Elem)
return i; return i;
} }
return -1; //-1 pq o elemento pode estar na posição zero
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment