Last active
July 17, 2021 12:49
-
-
Save davidalencar/c5cd6411ca9b018c28027eb869b760d8 to your computer and use it in GitHub Desktop.
Golang program for implementation of Binary Search
This file contains hidden or 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 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