|
|
|
@ -55,13 +55,51 @@ comments: true
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="counting_sort.cpp"
|
|
|
|
|
[class]{}-[func]{countingSortNaive}
|
|
|
|
|
/* 计数排序 */
|
|
|
|
|
// 简单实现,无法用于排序对象
|
|
|
|
|
void countingSortNaive(vector<int>& nums) {
|
|
|
|
|
// 1. 统计数组最大元素 m
|
|
|
|
|
int m = 0;
|
|
|
|
|
for (int num : nums) {
|
|
|
|
|
m = max(m, num);
|
|
|
|
|
}
|
|
|
|
|
// 2. 统计各数字的出现次数
|
|
|
|
|
// counter[num] 代表 num 的出现次数
|
|
|
|
|
vector<int> counter(m + 1, 0);
|
|
|
|
|
for (int num : nums) {
|
|
|
|
|
counter[num]++;
|
|
|
|
|
}
|
|
|
|
|
// 3. 遍历 counter ,将各元素填入原数组 nums
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (int num = 0; num < m + 1; num++) {
|
|
|
|
|
for (int j = 0; j < counter[num]; j++, i++) {
|
|
|
|
|
nums[i] = num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="counting_sort.py"
|
|
|
|
|
[class]{}-[func]{counting_sort_naive}
|
|
|
|
|
def counting_sort_naive(nums: List[int]) -> None:
|
|
|
|
|
""" 计数排序 """
|
|
|
|
|
# 简单实现,无法用于排序对象
|
|
|
|
|
# 1. 统计数组最大元素 m
|
|
|
|
|
m = 0
|
|
|
|
|
for num in nums:
|
|
|
|
|
m = max(m, num)
|
|
|
|
|
# 2. 统计各数字的出现次数
|
|
|
|
|
# counter[num] 代表 num 的出现次数
|
|
|
|
|
counter = [0] * (m + 1)
|
|
|
|
|
for num in nums:
|
|
|
|
|
counter[num] += 1
|
|
|
|
|
# 3. 遍历 counter ,将各元素填入原数组 nums
|
|
|
|
|
i = 0
|
|
|
|
|
for num in range(m + 1):
|
|
|
|
|
for _ in range(counter[num]):
|
|
|
|
|
nums[i] = num
|
|
|
|
|
i += 1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
@ -213,13 +251,67 @@ $$
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="counting_sort.cpp"
|
|
|
|
|
[class]{}-[func]{countingSort}
|
|
|
|
|
/* 计数排序 */
|
|
|
|
|
// 完整实现,可排序对象,并且是稳定排序
|
|
|
|
|
void countingSort(vector<int>& nums) {
|
|
|
|
|
// 1. 统计数组最大元素 m
|
|
|
|
|
int m = 0;
|
|
|
|
|
for (int num : nums) {
|
|
|
|
|
m = max(m, num);
|
|
|
|
|
}
|
|
|
|
|
// 2. 统计各数字的出现次数
|
|
|
|
|
// counter[num] 代表 num 的出现次数
|
|
|
|
|
vector<int> counter(m + 1, 0);
|
|
|
|
|
for (int num : nums) {
|
|
|
|
|
counter[num]++;
|
|
|
|
|
}
|
|
|
|
|
// 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引”
|
|
|
|
|
// 即 counter[num]-1 是 num 在 res 中最后一次出现的索引
|
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
|
|
|
counter[i + 1] += counter[i];
|
|
|
|
|
}
|
|
|
|
|
// 4. 倒序遍历 nums ,将各元素填入结果数组 res
|
|
|
|
|
// 初始化数组 res 用于记录结果
|
|
|
|
|
int n = nums.size();
|
|
|
|
|
vector<int> res(n);
|
|
|
|
|
for (int i = n - 1; i >= 0; i--) {
|
|
|
|
|
int num = nums[i];
|
|
|
|
|
res[counter[num] - 1] = num; // 将 num 放置到对应索引处
|
|
|
|
|
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
|
|
|
|
|
}
|
|
|
|
|
// 使用结果数组 res 覆盖原数组 nums
|
|
|
|
|
nums = res;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="counting_sort.py"
|
|
|
|
|
[class]{}-[func]{counting_sort}
|
|
|
|
|
def counting_sort(nums: List[int]) -> None:
|
|
|
|
|
""" 计数排序 """
|
|
|
|
|
# 完整实现,可排序对象,并且是稳定排序
|
|
|
|
|
# 1. 统计数组最大元素 m
|
|
|
|
|
m = max(nums)
|
|
|
|
|
# 2. 统计各数字的出现次数
|
|
|
|
|
# counter[num] 代表 num 的出现次数
|
|
|
|
|
counter = [0] * (m + 1)
|
|
|
|
|
for num in nums:
|
|
|
|
|
counter[num] += 1
|
|
|
|
|
# 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引”
|
|
|
|
|
# 即 counter[num]-1 是 num 在 res 中最后一次出现的索引
|
|
|
|
|
for i in range(m):
|
|
|
|
|
counter[i + 1] += counter[i]
|
|
|
|
|
# 4. 倒序遍历 nums ,将各元素填入结果数组 res
|
|
|
|
|
# 初始化数组 res 用于记录结果
|
|
|
|
|
n = len(nums)
|
|
|
|
|
res = [0] * n
|
|
|
|
|
for i in range(n - 1, -1, -1):
|
|
|
|
|
num = nums[i]
|
|
|
|
|
res[counter[num] - 1] = num # 将 num 放置到对应索引处
|
|
|
|
|
counter[num] -= 1 # 令前缀和自减 1 ,得到下次放置 num 的索引
|
|
|
|
|
# 使用结果数组 res 覆盖原数组 nums
|
|
|
|
|
for i in range(n):
|
|
|
|
|
nums[i] = res[i]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|