feat(counting_sort): support counting_sort in c/go (#431)

* feat(go/counting_sort): support counting_sort in go

* feat(test): support counting_sort_naive testcase

* feat(go/counting_sort): support counting sort

* feat(c/counting_sort): support counting_sort in c
This commit is contained in:
Reanon
2023-03-20 21:16:25 +08:00
committed by GitHub
parent ecc718d0b1
commit c837882dbd
5 changed files with 168 additions and 1 deletions

View File

@ -0,0 +1,20 @@
// File: count_sort_test.go
// Created Time: 2023-03-20
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestCountingSort(t *testing.T) {
c := &CountingSort{}
nums := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
c.countingSortNaive(nums)
fmt.Println("计数排序(无法排序对象)完成后 nums = ", nums)
c.countingSort(nums)
fmt.Println("计数排序完成后 nums = ", nums)
}

View File

@ -0,0 +1,66 @@
// File: counting_sort.go
// Created Time: 2023-03-20
// Author: Reanon (793584285@qq.com)
package chapter_sorting
type CountingSort struct{}
/* 计数排序 */
// 简单实现,无法用于排序对象
func (c *CountingSort) 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++
}
}
}
func (c *CountingSort) 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)
}