-
Notifications
You must be signed in to change notification settings - Fork 107
/
p4.go
53 lines (48 loc) · 1018 Bytes
/
p4.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
// Copyright 2020, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package main
import (
"fmt"
//"sort"
)
// 把 sort.Search 拷过来,加些单步日志
func SearchWithLog(n int, f func(int) bool) int {
i, j := 0, n
for i < j {
h := int(uint(i+j) >> 1)
fmt.Printf("i=%d, j=%d, h=%d", i, j, h)
if !f(h) {
fmt.Println(" not match.")
i = h + 1
} else {
fmt.Println("match.")
j = h
}
}
return i
}
func main() {
const key = 3
//const key = 0
//const key = 2
//const key = 9
arr := []int{1, 3, 5, 7}
index := SearchWithLog(len(arr), func(i int) bool {
//return arr[i] == 3
return arr[i] >= key
})
fmt.Printf("index=%d\n", index)
if index < len(arr) && arr[index] == key {
fmt.Println("exist")
} else {
fmt.Println("not exist")
}
//var tmp uint
//tmp = uint(2147483647 + 2147483647)
//fmt.Println(tmp)
}