Last active
July 24, 2017 18:59
-
-
Save sklrsn/8b0ed7fd840534b4a1c48223fa8df31e to your computer and use it in GitHub Desktop.
Binary Search Implementation in Golang
This file contains 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
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