mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-13 02:38:34 +08:00
build
This commit is contained in:
@ -67,7 +67,30 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="counting_sort.go"
|
||||
[class]{}-[func]{countingSortNaive}
|
||||
/* 计数排序 */
|
||||
// 简单实现,无法用于排序对象
|
||||
func countingSortNaive(nums []int) {
|
||||
// 1. 统计数组最大元素 m
|
||||
m := 0
|
||||
for num := range nums {
|
||||
if num > m {
|
||||
m = num
|
||||
}
|
||||
}
|
||||
// 2. 统计各数字的出现次数
|
||||
// counter[num] 代表 num 的出现次数
|
||||
counter := make([]int, m+1)
|
||||
for _, num := range nums {
|
||||
counter[num]++
|
||||
}
|
||||
// 3. 遍历 counter ,将各元素填入原数组 nums
|
||||
for i, num := 0, 0; num < m+1; num++ {
|
||||
for j := 0; j < counter[num]; j++ {
|
||||
nums[i] = num
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -202,7 +225,41 @@ $$
|
||||
=== "Go"
|
||||
|
||||
```go title="counting_sort.go"
|
||||
[class]{}-[func]{countingSort}
|
||||
/* 计数排序 */
|
||||
// 完整实现,可排序对象,并且是稳定排序
|
||||
func countingSort(nums []int) {
|
||||
// 1. 统计数组最大元素 m
|
||||
m := 0
|
||||
for num := range nums {
|
||||
if num > m {
|
||||
m = num
|
||||
}
|
||||
}
|
||||
// 2. 统计各数字的出现次数
|
||||
// counter[num] 代表 num 的出现次数
|
||||
counter := make([]int, m+1)
|
||||
for _, num := range nums {
|
||||
counter[num]++
|
||||
}
|
||||
// 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引”
|
||||
// 即 counter[num]-1 是 num 在 res 中最后一次出现的索引
|
||||
for i := 0; i < m; i++ {
|
||||
counter[i+1] += counter[i]
|
||||
}
|
||||
// 4. 倒序遍历 nums ,将各元素填入结果数组 res
|
||||
// 初始化数组 res 用于记录结果
|
||||
n := len(nums)
|
||||
res := make([]int, n)
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
num := nums[i]
|
||||
// 将 num 放置到对应索引处
|
||||
res[counter[num]-1] = num
|
||||
// 令前缀和自减 1 ,得到下次放置 num 的索引
|
||||
counter[num]--
|
||||
}
|
||||
// 使用结果数组 res 覆盖原数组 nums
|
||||
copy(nums, res)
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
Reference in New Issue
Block a user