Skip to content

Instantly share code, notes, and snippets.

@agbaraka
Created September 6, 2017 07:14
Show Gist options
  • Save agbaraka/e1f2558ea064f8e5e2043802a2b75767 to your computer and use it in GitHub Desktop.
Save agbaraka/e1f2558ea064f8e5e2043802a2b75767 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func binarySearch(needle int, haystack []int) bool {
low := 0
high := len(haystack) - 1
for low <= high{
median := (low + high) / 2
if haystack[median] < needle {
low = median + 1
}else{
high = median - 1
}
}
if low == len(haystack) || haystack[low] != needle {
return false
}
return true
}
func main(){
numbers := []int{1,2, 9, 20, 31, 45, 63, 70, 100}
fmt.Println(binarySearch(63, numbers))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment