Created
June 2, 2011 23:44
-
-
Save kaldas/1005590 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* $ trab_busca_binaria.c | |
| * | |
| * Aluno: Rodrigo Caldas de Moura Duarte | |
| * Prof.: Marcelo Vasconcelos | |
| * Objetivo: Implementar busca binária em C | |
| */ | |
| #include "stdio.h" | |
| #include "stdlib.h" | |
| int main() | |
| { | |
| int array[] = {2, 19, 25, 35, 38, 47, 77, 78, 158, 178, 214, 215, 329, 487, 500}; | |
| int esquerda = 0, | |
| direita = (sizeof(array) / sizeof(int)) - 1, /* subtrai 1 pois enumeracao comeca de 0 e size = tamanho */ | |
| metade = 0, | |
| procurado = 0, | |
| sinal = 0; | |
| printf("Entre com o número a ser buscado: "); | |
| scanf("%i", &procurado); | |
| printf("\n\nIniciando busca..\n\n"); | |
| for(metade = (direita + esquerda) / 2; esquerda <= direita; metade = (esquerda + direita) / 2) | |
| { | |
| if(array[metade] == procurado) | |
| { | |
| printf("\nNumero procurado [%i] foi encontrado na posicao: %i\n", procurado, metade); | |
| sinal = 1; | |
| break; | |
| } else { | |
| if(array[metade] > procurado) | |
| { | |
| direita = metade - 1; | |
| } else { | |
| esquerda = metade + 1; | |
| } | |
| } | |
| } | |
| if(sinal == 0) | |
| printf("\nSeu numero [%i] nao estava no array!\n",procurado); | |
| return 0x101; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment