Skip to content

Instantly share code, notes, and snippets.

@sklrsn
Last active July 24, 2017 18:59
Show Gist options
  • Save sklrsn/8b0ed7fd840534b4a1c48223fa8df31e to your computer and use it in GitHub Desktop.
Save sklrsn/8b0ed7fd840534b4a1c48223fa8df31e to your computer and use it in GitHub Desktop.
Binary Search Implementation in Golang
package main
import "fmt"
func Binarysearch(a []int, key int) bool {
left := 0
right := len(a)
for left <= right {
mid := (left + right) / 2
if a[mid] == key {
return true
} else if a[mid] > key {
right = mid - 1
} else {
left = mid + 1
}
}
return false
}
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(Binarysearch(arr, 5))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment