package main
import "fmt"
func main() {
println("冒泡排序")
testArr := []int{23,12,34,6,8,342,234,8677,33,22,88}
fmt.Println("排序前",testArr)
bubbleSort2(testArr)
fmt.Println("排序后",testArr)
}
//冒泡排序
func bubbleSort(needSortArr []int){
arrLength := len(needSortArr)
for i := 0;i < arrLength - 1; i++ {
for k := 0; k < arrLength - 1 - i; k++ {
if needSortArr[k] > needSortArr[k+1] {
needSortArr[k] = needSortArr[k] + needSortArr[k+1]
needSortArr[k+1] = needSortArr[k] - needSortArr[k+1]
needSortArr[k] = needSortArr[k] - needSortArr[k+1]
}
}
}
}
//冒泡排序
func bubbleSort2(needSortArr []int){
arrLength := len(needSortArr)
for i := 0;i < arrLength - 1; i++ {
for k := 0; k < arrLength - 1 - i; k++ {
if needSortArr[k] > needSortArr[k+1] {
needSortArr[k] , needSortArr[k+1] = needSortArr[k+1] , needSortArr[k]
}
}
}
}
网友评论