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
static int busquedaBinaria(int[] arreglo, int busqueda, int izquierda, int derecha) | |
{ | |
if (izquierda > derecha) | |
{ | |
return -1; | |
} | |
// Comprobar si está en el centro | |
int indiceCentral = Convert.ToInt32(Math.Floor(Convert.ToDouble(izquierda + derecha) / 2)); | |
int valorCentral = arreglo[indiceCentral]; | |
if (valorCentral == busqueda) | |
{ | |
return indiceCentral; | |
} | |
// Si no, debido a que esto ya está ordenado, analizamos dónde podría estar | |
if (busqueda < valorCentral) | |
{ | |
// Si lo que buscamos es menor, debe estar a la izquierda | |
derecha = indiceCentral - 1; | |
} | |
else | |
{ | |
// Si no, está a la derecha | |
izquierda = indiceCentral + 1; | |
} | |
return busquedaBinaria(arreglo, busqueda, izquierda, derecha); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment