golang 冒泡排序

来源:互联网 发布:域名举报需要多少人 编辑:程序博客网 时间:2024/06/06 05:28


package mainimport ("fmt")func main() {var a = [...]int{9, 1, 4, 8}fmt.Printf("the array len is %v \n", len(a))// for i := 0; i < len(a); i++ {// for j := i; j < len(a)-1; j++ {// fmt.Printf("the index1 is %v \n", j)// if a[j+1] < a[j] {// fmt.Printf("the index2 is %v \n", j)// var temp int// //按从小到大// temp = a[j]// a[j] = a[j+1]// a[j+1] = temp// }// }// }// for i := 0; i < len(a); i++ {// for j := 0; j < len(a)-i-1; j++ {// fmt.Printf("the index1 is %v \n", j)// if a[j+1] < a[j] {// fmt.Printf("the index2 is %v \n", j)// var temp int// //按从小到大// temp = a[j]// a[j] = a[j+1]// a[j+1] = temp// }// }// }// for i := 0; i < len(a); i++ {// for j := 0; j < len(a)-i-1; j++ {// fmt.Printf("the index1 is %v \n", j)// if a[j+1] < a[j] {// fmt.Printf("the index2 is %v \n", j)// // var temp int// // //按从小到大// // temp = a[j]// // a[j] = a[j+1]// // a[j+1] = temp// a[j], a[j+1] = a[j+1], a[j]// }// }// }// for i := 0; i < len(a); i++ {// for j := 0; j < len(a); j++ {// fmt.Printf("the index1 is %v \n", j)// if j+1 < len(a) {// // fmt.Printf("the index2 is %v \n", j)// // a[j], a[j+1] = a[j+1], a[j]// if a[j+1] < a[j] {// fmt.Printf("the index2 is %v \n", j)// a[j], a[j+1] = a[j+1], a[j]// }// }// }// }// for range a {// for j, _ := range a {// if j+1 < len(a) {// if a[j+1] < a[j] {// fmt.Printf("the index2 is %v \n", j)// a[j], a[j+1] = a[j+1], a[j]// }// fmt.Print(j)// fmt.Print("\n")// }// }// }for i := 0; i < len(a); i++ {for j := i; j < len(a); j++ {var temp intif a[j] < a[i] {temp = a[i]a[i] = a[j]a[j] = temp}}}// for i := 0; i < len(a); i++ {// for j := i; j < len(a); j++ {// if a[j] < a[i] {// a[i], a[j] = a[j], a[i]// }// }// }fmt.Printf("the array is %v \n", a)}


0 0
原创粉丝点击