From 1310b19d05cb8937ded5572997b10356976ce56e Mon Sep 17 00:00:00 2001 From: krahets Date: Mon, 20 Mar 2023 21:40:04 +0800 Subject: [PATCH] build --- chapter_sorting/counting_sort.md | 61 ++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/chapter_sorting/counting_sort.md b/chapter_sorting/counting_sort.md index 4d675c3e0..9a32da259 100644 --- a/chapter_sorting/counting_sort.md +++ b/chapter_sorting/counting_sort.md @@ -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"