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.
 
 
 
 
 
 

38 lines
678 B

package main
/***
希尔排序
*/
func main() {
var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明
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张小,则循环交换较大的值
swap1(s, j, j-get)
}
}
}
func swap1(slice []int, i int, j int) {
slice[i], slice[j] = slice[j], slice[i]
}