package main
import(
"fmt"
)
func BinarySearch(arr []int, target int) (int,bool){
left := 0 //左下标
right := len(arr) - 1
for left <= right{
mid := (left + right)/2
if arr[mid] > target{ //说明目标值在左半边
right = mid -1
}else if arr[mid] < target{ //说明在右半边
left = mid + 1
}else if arr[mid] == target{
return mid, true
}
}
return -1, false
}
func main(){
arr := []int{0,2,5,8,9}
index, ok := BinarySearch(arr, 9)
if ok == true{
fmt.Println("索引值是:", index)
}else {
fmt.Println("未发现目标值")
}
}