You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

29 lines
532 B

package main
// 插入排序
func main() {
var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明
Insert(a)
for _, i2 := range a {
print(i2, "\t") // 输出每一个值
}
}
/**
Insert 实现插入排序 O(n * n)
a : 被排序数组
返回:排序完毕数组
*/
func Insert(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-- {
// 选择第j张比较第j - 1张小,则循环交换较大的值
s[j], s[j-1] = s[j-1], s[j]
}
}
}