Go:二分查找
package main import "fmt" func BinarySearch(arr *[5]int, leftIndex int, rightIndex int, findValue int) { // 判断leftIndex是否大于rightIndex if leftIndex > rightIndex { fmt.Println("找不到...") return } middleIndex := (leftIndex + rightIndex) / 2 if (*arr)[middleIndex] > findValue { // 说明要找的数,在 leftIndex 至 middleIndex-1 BinarySearch(arr, leftIndex, middleIndex-1, findValue) } else if (*arr)[middleIndex] < findValue { // 说明要找的数,在 middleIndex+1 至 rightIndex BinarySearch(arr, middleIndex+1, rightIndex, findValue) } else { // 找到了 fmt.Printf("找到了,下标为%d", middleIndex) } } func main() { arr := [5]int{1, 5, 17, 22, 68} BinarySearch(&arr, 0, len(arr), 22) }