File tree 2 files changed +22
-23
lines changed
2 files changed +22
-23
lines changed Original file line number Diff line number Diff line change 1
- module github.com/skurhse/gresto /binsrch
1
+ module github.com/skurhse/fisher/modules /binsrch
2
2
3
3
go 1.22.3
Original file line number Diff line number Diff line change 1
1
package binsrch
2
2
3
3
func Search (nums []int , target int ) int {
4
- var find func (int , int ) int
5
- find = func (l int , u int ) int {
6
-
7
- window := nums [l :u ]
8
- size := len (window )
4
+ return searchWindow (0 , len (nums ), nums , target )
5
+ }
9
6
10
- if size == 1 {
11
- if window [0 ] == target {
12
- return l
13
- } else {
14
- return - 1
15
- }
16
- }
7
+ func searchWindow (l int , u int , nums []int , target int ) int {
8
+ window := nums [l :u ]
17
9
18
- j := size / 2
19
- e := window [j ]
10
+ size := len (window )
20
11
21
- switch {
22
- case e == target :
23
- return l + j
24
- case e < target :
25
- return find (l + j , u )
26
- default :
27
- return find (l , u - j )
12
+ if size == 1 {
13
+ if window [0 ] == target {
14
+ return l
15
+ } else {
16
+ return - 1
28
17
}
29
18
}
30
19
31
- return find (0 , len (nums ))
20
+ j := size / 2
21
+ e := window [j ]
22
+
23
+ switch {
24
+ case e == target :
25
+ return l + j
26
+ case e < target :
27
+ return searchWindow (l + j , u , nums , target )
28
+ default :
29
+ return searchWindow (l , u - j , nums , target )
30
+ }
32
31
}
You can’t perform that action at this time.
0 commit comments