-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinary-search.go
41 lines (34 loc) · 982 Bytes
/
binary-search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import "fmt"
func main() {
var lookingFor int = 6
var sortedList []int = []int{1, 3, 4, 6, 7, 9, 10, 11, 13}
fmt.Println("Looking for", lookingFor, "in the sorted list:", sortedList)
index := binarySearch(sortedList, lookingFor)
if index >= 0 {
fmt.Println("Found the number", lookingFor, "at:", index)
} else {
fmt.Println("Didn't find the number", lookingFor, ":(")
}
}
func binarySearch(sortedList []int, lookingFor int) int {
var lo int = 0
var hi int = len(sortedList) - 1
for lo <= hi {
var mid int = lo + (hi-lo)/2
var midValue int = sortedList[mid]
fmt.Println("Middle value is:", midValue)
if midValue == lookingFor {
return mid
} else if midValue > lookingFor {
// We want to use the left half of our list
hi = mid - 1
} else {
// We want to use the right half of our list
lo = mid + 1
}
}
// If we get here we tried to look at an invalid sub-list
// which means the number isn't in our list.
return -1
}