Created
July 13, 2019 16:33
-
-
Save MrDave1999/1cdd65c6e570fc3ff1d1227a123c2794 to your computer and use it in GitHub Desktop.
Función iterativa donde se implementa una búsqueda binaria.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
int BBIterative(int* pv, int data, int size) | |
{ | |
int first = 0; | |
int end = size-1; | |
int medium; | |
unsigned int i = 0; | |
while(first <= end) | |
{ | |
++i; | |
medium = (first + end) /2; | |
if(*(pv + medium) == data) | |
{ | |
printf("Iteraciones: %d\n", i); | |
return medium; | |
} | |
(data > *(pv + medium)) ? (first = medium + 1) : (end = medium - 1); | |
} | |
printf("Iteraciones: %d\n", i); | |
return -1; | |
} | |
int main(void) | |
{ | |
int arr[MAX_ELEMENTS]; | |
int data; | |
for(unsigned int i = 0; i != MAX_ELEMENTS; ++i) | |
arr[i] = i; | |
printf("Ingrese un valor\n"); | |
scanf("%d", &data); | |
printf("El numero %d %s se encuentra en el arreglo.\n", data, (BBIterative(arr, data, MAX_ELEMENTS) == -1) ? ("no") : ("si")); | |
getchar(); | |
getchar(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment