package main
import "fmt"
func main() {
arr := [5]int{24, 69, 80, 57, 13}
arr1 := [5]int{12, 23, 45, 56, 88}
BubbleSort(&arr)
BinaryFind(&arr1, 0, len(arr1)-1, 45)
fmt.Println("排序后", arr)
}
func BinaryFind(arr *[5]int, leftIndex int, rightIndex int, findVal int) {
if leftIndex > rightIndex {
fmt.Println("找不到")
return
}
mid := (leftIndex + rightIndex) / 2
if (*arr)[mid] > findVal {
BinaryFind(arr, leftIndex, mid-1, findVal)
} else if (*arr)[mid] < findVal {
BinaryFind(arr, mid+1, rightIndex, findVal)
} else {
fmt.Printf("找到了下标%v\n", mid)
}
}
func BubbleSort(arr *[5]int) {
fmt.Println("排序前", (*arr))
var temp = 0
for i := 0; i < len(arr)-1; i++ {
for j := 0; j < len(arr)-i-1; j++ {
if (*arr)[j] > arr[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45