go语言基础之冒泡排序原理

时间:2021-11-19 15:09:01

1、冒泡排序原理

go语言基础之冒泡排序原理

示例:

package main //必须有个main包

import "fmt"
import "math/rand"
import "time" func main() {
//设置种子, 只需一次
rand.Seed(time.Now().UnixNano()) var a [10]int
n := len(a) for i := 0; i < n; i++ {
a[i] = rand.Intn(100) //100以内的随机数
fmt.Printf("%d, ", a[i])
}
fmt.Printf("\n") //冒泡排序,挨着的2个元素比较,升序(大于则交换)
for i := 0; i < n-1; i++ {
for j := 0; j < n-1-i; j++ {
if a[j] > a[j+1] {
a[j], a[j+1] = a[j+1], a[j]
}
}
} fmt.Printf("\n排序后:\n")
for i := 0; i < n; i++ {
fmt.Printf("%d, ", a[i])
}
fmt.Printf("\n")
}

执行结果:

62, 88, 48, 10, 70, 74, 89, 19, 67, 41, 

排序后:
10, 19, 41, 48, 62, 67, 70, 74, 88, 89,