package main import "fmt" func main() { // 冒泡排序 arr := [...]int{5, 7, 2, 1, 6, 4, 8, 9, 0, 3} fmt.Print("排序前") fmt.Println(arr) length := len(arr) for i := 0; i < length-1; i++ { for j := 0; j < length-i-1; j++ { if arr[j] > arr[j+1] { temp := arr[j] arr[j] = arr[j+1] arr[j+1] = temp } } } fmt.Print("排序后") fmt.Println(arr) }
输出:
排序前[5 7 2 1 6 4 8 9 0 3]
排序后[0 1 2 3 4 5 6 7 8 9]