Created
August 7, 2019 16:37
-
-
Save parzibyte/2231b1a1c90cddc8edc52765bfcdf97c to your computer and use it in GitHub Desktop.
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
func busquedaBinariaCadenas(arreglo []string, busqueda string) (indice int) { | |
izquierda := 0 | |
derecha := len(arreglo) - 1 | |
/* | |
Recordemos que For is Go's "while" | |
https://tour.golang.org/flowcontrol/3 | |
*/ | |
for izquierda <= derecha { | |
indiceDelMedio := int(math.Floor((float64(izquierda+derecha) / 2))) | |
elementoDelMedio := arreglo[indiceDelMedio] | |
if elementoDelMedio == busqueda { | |
return indiceDelMedio | |
} | |
if busqueda < elementoDelMedio { | |
derecha = indiceDelMedio - 1 | |
} else { | |
izquierda = indiceDelMedio + 1 | |
} | |
} | |
return -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment