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

Busca Binaria Implementada

parent af2b9b42
No related branches found
No related tags found
No related merge requests found
No preview for this file type
#include "Estruturas.h"
void LerDados (int *Vetor);
void GeraVetor (int *Vetor);
void ImprimeVetor (int *Vetor);
void SelectSort (int *Vetor);
void VerificaNumero (int *Vetor, int *Escolhidos);
void PesquisaBinaria (int *Vetor, int Elem);
int main () {
int Vetor[TAM];
int Vetor[TAM], Escolhidos[4];
LerDados (Escolhidos);
GeraVetor(Vetor);
ImprimeVetor(Vetor);
SelectSort (Vetor);
ImprimeVetor (Vetor);
VerificaNumero (Vetor, Escolhidos);
}
//Arquivo com os algoritmos de busca
#include "Estruturas.h"
int PesquisaBinaria (int *Vetor, int Elem) {
int meio, esquerda, direita;
esquerda = 0;
direita = TAM;
do {
meio = (esquerda + direita)/2;
if (Elem > Vetor[meio])
esquerda = meio + 1;
else
direita = meio - 1;
} while (Elem != Vetor[meio] && esquerda <= direita);
if (Elem == Vetor [meio])
return (meio);
else
return -1;
}
void VerificaNumero (int *Vetor, int *Escolhidos) {
int i, acertos;
acertos = 0;
for (i = 0; i < 4; i++)
if (PesquisaBinaria (Vetor, Escolhidos[i]) != -1) {
printf ("Acertou o Numero %d, Parabéns\n", Escolhidos[i]);
acertos++;
}
printf ("O Total de Acertos foi de: %d\n", acertos);
}
#include "Estruturas.h"
void LerDados (int *Vetor) {
int i;
printf ("Digite 4 números: ");
for (i = 0; i < 4; i++)
scanf ("%d", &Vetor[i]);
}
//Função que gera valores aleatorios para o vetor
void GeraVetor (int *Vetor) {
int i;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment