Created
October 28, 2021 01:04
-
-
Save dilakv/4f4767db72f83a75b1cd87b80f786cda to your computer and use it in GitHub Desktop.
Búsqueda binaria de un número entre el mínimo y el máximo
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
func busqueda_binaria(min uint64, max uint64, item uint64) uint64 { | |
var intentos uint64 = 0; | |
if(item < min || item > max){ | |
return 0 | |
} | |
for min <= max { | |
intentos++ | |
var medio uint64 = (min + max) / 2 | |
var seraestenumero = medio | |
if seraestenumero == item { | |
fmt.Printf("Se logro en %v intentos, el número es %v", intentos, seraestenumero) | |
return seraestenumero | |
} | |
if seraestenumero > item { | |
max = medio - 1 | |
}else{ | |
min = medio + 1 | |
} | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment