package main import ( "fmt" ) func main() { a := [...]int{1, 3, 7, 4, 5, 6, 8} fmt.Println(a) var num int num = len(a) for i := 0; i < num; i++ { for j := i + 1; j < num-1; j++ { if a[i] > a[j] { temp := a[i] a[i] = a[j] a[j] = temp } } } fmt.Println(a) }或者
package main import ( "fmt" ) func main() { a := [...]int{1, 3, 7, 4, 5, 6, 8} fmt.Println(a) var num int num = len(a) for i := 1; i < num; i++ { for j := 0; j < num-i; j++ { if a[j] > a[j+1] { temp := a[j] a[j] = a[j+1] a[j+1] = temp } } } fmt.Println(a) }
有疑问加站长微信联系(非本文作者)