Skip to content

Instantly share code, notes, and snippets.

@davidalencar
Last active July 17, 2021 12:49
Show Gist options
  • Save davidalencar/c5cd6411ca9b018c28027eb869b760d8 to your computer and use it in GitHub Desktop.
Save davidalencar/c5cd6411ca9b018c28027eb869b760d8 to your computer and use it in GitHub Desktop.
Golang program for implementation of Binary Search
package main
import "fmt"
func binarySerach(list []int, item int) (bool, int) {
low := 0
high := len(list) - 1
for low <= high {
median := (low + high) / 2
if list[median] == item {
return true, median
} else if list[median] < item {
low = median + 1
} else {
high = median - 1
}
}
return false, -1
}
func main() {
list := []int{2, 3, 5, 7, 11, 13, 17, 19}
exist, index := binarySerach(list, 7)
fmt.Println(exist, index)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment