forked from go/golangs_learn
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.
32 lines
654 B
32 lines
654 B
package main
|
|
|
|
// 桶排序 - 失败
|
|
func main() {
|
|
var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明
|
|
Bucket(a, len(a), 50)
|
|
|
|
for _, i2 := range a {
|
|
print(i2, "\t") // 输出每一个值
|
|
}
|
|
}
|
|
|
|
/**
|
|
Bucket 实现桶排序
|
|
*/
|
|
func Bucket(li []int, n, max int) {
|
|
// 创建桶
|
|
a, b := make([][]int, n), max/n // a 桶 b 单桶的大小
|
|
for i := range a {
|
|
a[i] = make([]int, 0)
|
|
}
|
|
for i := 0; i < n; i++ {
|
|
n := li[i] / b
|
|
a[n] = append(a[n], li[i])
|
|
}
|
|
|
|
for i, j := 0, n; i <= n; i = i + 1 { // 因为存在递归,将数值填回数组
|
|
for ii, jj := 0, len(a[i]); ii < jj; ii, j = ii+1, j+1 {
|
|
li[j] = a[i][ii]
|
|
}
|
|
}
|
|
}
|
|
|