-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarysearch.go
71 lines (60 loc) · 1.73 KB
/
binarysearch.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Package binarysearch implements binary search
//
// Data structure Array
// Best-case performance O(1)
// Average performance O(log n)
// Worst-case performance O(log n)
// Worst-case space complexity O(1)
//
package binarysearch
// Ints searches for needle in data not sorted slice of ints and returns the index
// as specified by Search. The return value is the index to insert needle if needle is
// not present (it could be len(data)).
// The slice must be sorted in ascending order.
func Ints(data []int, needle int) int {
left := 0
right := len(data)
if right == 0 || needle < data[0] || needle > data[right-1] {
return -1 // Out of range
}
for left < right {
mid := left + (right-left)/2
if needle <= data[mid] {
right = mid
} else {
left = mid + 1
}
}
if data[right] == needle {
return right
}
return -1 // Not found
}
// IntsRecursion searches for needle in data not sorted slice of ints and returns the index
// as specified by Search. The return value is the index to insert needle if needle is
// not present (it could be len(data)).
// The slice must be sorted in ascending order.
func IntsRecursion(data []int, needle int) int {
left := 0
right := len(data)
if right == 0 || needle < data[left] || needle > data[right-1] {
return -1 // Out of range
}
return intsRecursion(data, needle, left, right-1)
}
func intsRecursion(data []int, needle int, left int, right int) int {
if left >= right {
if data[left] == needle {
return left
}
return -1 // Not found
}
mid := left + (right-left)/2
if v := data[mid]; v > needle {
return intsRecursion(data, needle, left, mid)
} else if v < needle {
return intsRecursion(data, needle, mid+1, right)
}
return mid
}