Skip to content

Instantly share code, notes, and snippets.

@RavenZZ
Created April 3, 2018 05:37
Show Gist options
  • Save RavenZZ/6beab993895d78cc676e5ef7d16bc511 to your computer and use it in GitHub Desktop.
Save RavenZZ/6beab993895d78cc676e5ef7d16bc511 to your computer and use it in GitHub Desktop.
A binary search is a search strategy used to find elements within a list by consistently reducing the amount of data to be searched and thereby increasing the rate at which the search term is found. To use a binary search algorithm, the list to be op
// Binary Search in Golang
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(){
items := []int{1,2, 9, 20, 31, 45, 63, 70, 100}
fmt.Println(binarySearch(63, items))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment