-
-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathbinary_search.go
More file actions
31 lines (24 loc) · 596 Bytes
/
Copy pathbinary_search.go
File metadata and controls
31 lines (24 loc) · 596 Bytes
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
// Description: Binary search algorithm implementation in Go
// Tags: binary, search, algorithm, slice, array, sort, sorted, sorted array, sorted slice, sorted
package main
import "fmt"
func binarySearch(element int, arr []int) bool {
low := 0
high := len(arr) - 1
for low <= high {
mid := (low + high) / 2
if arr[mid] < element {
low = mid + 1
} else {
high = mid - 1
}
}
if low == len(arr) || arr[low] != element {
return false
}
return true
}
func main() {
arr := []int{1, 4, 5, 7, 9, 10, 35, 56, 79, 80, 100, 200, 210, 250}
fmt.Println(binarySearch(9, arr))
}