package main import ( "math/rand" "time" "fmt" ) func ButtleData(s []int) { n := len(s) for i := 0; i < n; i++ { for j := 0; j < n-1-i; j++ { if s[j] > s[j+1] { s[j], s[j+1] = s[j+1], s[j] } } } } func InitData(s []int) { rand.Seed(time.Now().UnixNano()) for i := 0; i < len(s); i++{ s[i] = rand.Intn(100) } } func main() { n := 10 s := make([]int, n) InitData(s) fmt.Printf("Before Sort: %v\n", s) ButtleData(s) fmt.Printf("After Sort: %v\n", s) }
本次执行的结果为
Before Sort: [74 37 85 93 7 5 54 72 44 83] After Sort: [5 7 37 44 54 72 74 83 85 93
每次的执行结果都是不一样的