Go语言之冒泡排序
package main
import(
"fmt"
)
func main(){
var arr [5]int = [5]int{24,69,57,80,13}
bubbleSort(&arr)
fmt.Println(arr)
}
func bubbleSort(arr *[5]int){
for i:=0;i<4;i++{
for j:=0;j<4-i;j++{
if (*arr)[j] > (*arr)[j+1]{
temp := (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
}
}
Go语言之二分查找
package main
import(
"fmt"
)
func main(){
var arr1 [5]int = [5]int{1,3,5,7,9}
binarySearch(&arr1,0,len(arr1)-1,8)
}
func binarySearch(arr *[5]int,left int,right int,x int){
midIndex := (left+right)/2
if left>right{
fmt.Println("找不到该数")
return
}
if((*arr)[midIndex] > x){
binarySearch(arr,left,midIndex-1,x)
}else if((*arr)[midIndex] < x){
binarySearch(arr,midIndex+1,right,x)
}else{
fmt.Println("找到了该值:",x,"下标为:",midIndex)
}
}