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.
28 lines
476 B
28 lines
476 B
package main
|
|
|
|
// 计数排序
|
|
func main() {
|
|
var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明
|
|
Count(a, 50)
|
|
|
|
for _, i2 := range a {
|
|
print(i2, "\t") // 输出每一个值
|
|
}
|
|
}
|
|
|
|
/**
|
|
Count 实现计数排序
|
|
限制:仅限百内之整数
|
|
*/
|
|
func Count(li []int, max int) {
|
|
a := make([]int, max)
|
|
for i := 0; i < len(li); i++ {
|
|
a[li[i]] += 1
|
|
}
|
|
|
|
for i, j := 0, 0; i < max; i++ {
|
|
for ii := a[i]; ii > 0; ii, j = ii-1, j+1 {
|
|
li[j] = i
|
|
}
|
|
}
|
|
}
|
|
|