ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
> ### 十四例 二分查找 * 有序表查找:二分查找(静态查找, 没有重复数据) * (选取中位数和要查找的数进行比较) ~~~ package main import ( "fmt" "math/rand" "sort" ) func BinarySearch(list []int, low, high, v int) (k int) { if low > high { return -1 } mid := (low + high) / 2 if list[mid-1] > v { return BinarySearch(list, low, mid-1, v) } else if list[mid-1] < v { return BinarySearch(list, mid+1, high, v) } else { return mid - 1 } } func main() { //rand.Seed(time.Now().Unix()) number := make([]int, 100, 1000000) for k, _ := range number { number[k] = rand.Intn(1000000) } sort.Ints(number) //TODO : 有序表查找:二分查找(静态查找, 没有重复数据) fmt.Println(BinarySearch(number, 1, len(number), 66831)) //fmt.Println(number) } ~~~