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.

35 lines
607 B

3 years ago
package main
3 years ago
/***
希尔排序
*/
func main() {
var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明
3 years ago
for d := len(a); d >= 1; d = d / 2 {
Shell(a, d)
}
for _, i2 := range a {
print(i2, "\t") // 输出每一个值
}
}
/**
Shell 实现希尔排序 O(n * n)
a : 被排序数组
返回排序完毕数组
*/
func Shell(s []int, get int) {
n := len(s)
if n < 2 {
return
}
for i := get; i < n; i++ {
for j := i; j > 0 && s[j] < s[j-get]; j = j - get {
// 选择第j张比较第j - get张小,则循环交换较大的值
s[j], s[j-get] = s[j-get], s[j]
3 years ago
}
}
}