package main import( "fmt" ) func main() { var arr [5]int = [5]int{11,13,9,2,25} maopao(&arr) fmt.Println("arr = ", arr) //[2 9 11 13 25] findIndex := binaryFind(&arr, 0, len(arr) - 1, 25) fmt.Println(findIndex) } func binaryFind(arr *[5]int, leftIndex int, rightIndex int, findVal int) int { if leftIndex > rightIndex { return -1 } middleIndex := (leftIndex + rightIndex) / 2 //找中間下標 if (*arr)[middleIndex] > findVal { //說明findVal在左邊 return binaryFind(arr, leftIndex, middleIndex - 1, findVal) } else if (*arr)[middleIndex] < findVal { //說明findVal在右邊 return binaryFind(arr, middleIndex + 1, rightIndex, findVal) } else { return middleIndex } } func maopao(arr *[5]int) { var temp int = 0 //臨時變量作交換 len := len(arr) for i := 0; i < len - 1; i++ { //外層循環長度減1 for j := 0; j < len - 1 - i; j++ { if arr[j] > (*arr)[j+1] { temp = (*arr)[j+1] (*arr)[j+1] = (*arr)[j] (*arr)[j] = temp } } } }