// InsertSort project main.go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var my_slice = make([]int, 0, 100)
rand.Seed(int64(time.Now().Nanosecond()))
for i := 0; i < 10; i++ {
my_slice = append(my_slice, rand.Intn(1001))
}
fmt.Println("Before The Insert Sort:")
fmt.Println(my_slice)
fmt.Println("Sorting......")
InsertSort(my_slice)
fmt.Println("OK! Finish!!!")
fmt.Println("After The Insert Sort:")
fmt.Println(my_slice)
}
func InsertSort(slice_arg []int) {
var tmpVar int
for j := 1; j < len(slice_arg); j++ {
tmpVar = slice_arg[j]
i := j - 1
//for tmpVar < slice_arg[i] {
// slice_arg[i+1] = slice_arg[i]
// i--
// if i == -1 {
// break
// }
//}
//for i >= 0 {
// if tmpVar < slice_arg[i] {
// slice_arg[i+1] = slice_arg[i]
// } else {
// break
// }
// i--
//}
for tmpVar < slice_arg[i] && i >= 0 { //这里怎么就不行呢
slice_arg[i+1] = slice_arg[i]
i--
}
slice_arg[i+1] = tmpVar
fmt.Println(slice_arg)
}
}
有疑问加站长微信联系(非本文作者)