封装成函数:
//插入排序
func InsertionSort(s []int) {
n := len(s)
if n < 2 {
return
}
for i := 1; i < n; i++ {
for j := i; j > 0 && s[j] < s[j - 1]; j-- {
swap(s, j, j - 1)
}
}
}
func swap(slice []int, i int, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
s := []int{9,0,6,5,8,2,1,7,4,3}
fmt.Println(s)
InsertionSort(s)
fmt.Println(s)
[9 0 6 5 8 2 1 7 4 3]
[0 1 2 3 4 5 6 7 8 9]
有疑问加站长微信联系(非本文作者)