package main
import "fmt"
var index int
func main() {
//有序数组
var array = [17]int{2, 5, 8, 14, 15, 18, 19, 20, 29, 34, 55, 56, 57, 58, 59, 60, 67}
var findValue int = 67
var findindex = BinarySearch(array[:], findValue)
fmt.Println(findindex)
fmt.Printf("二分查找执行次数:%d,查找到的下标索引:%d", index, findindex)
}
func BinarySearch(array []int, value int) int {
index++
mindLength := len(array) / 2
var mindValue = array[mindLength]
if mindValue == value {
return mindLength
} else if mindValue > value {
return BinarySearch(array[:mindLength], value)
} else {
starIndex := mindLength + 1
return starIndex + BinarySearch(array[starIndex:], value)
}
}
标签:二分,mindLength,int,value,BinarySearch,var,查找,GO,array
From: https://www.cnblogs.com/jlion/p/16789983.html