78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
|
/**
|
|||
|
* File: counting_sort.cpp
|
|||
|
* Created Time: 2023-03-17
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
// <20><><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void countingSortNaive(vector<int> &nums) {
|
|||
|
// 1. ͳ<><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> m
|
|||
|
int m = 0;
|
|||
|
for (int num : nums) {
|
|||
|
m = max(m, num);
|
|||
|
}
|
|||
|
// 2. ͳ<>Ƹ<EFBFBD><C6B8><EFBFBD><EFBFBD>ֵij<D6B5><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
|
|||
|
// counter[num] <20><><EFBFBD><EFBFBD> num <20>ij<EFBFBD><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
|
|||
|
vector<int> counter(m + 1, 0);
|
|||
|
for (int num : nums) {
|
|||
|
counter[num]++;
|
|||
|
}
|
|||
|
// 3. <20><><EFBFBD><EFBFBD> counter <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD> nums
|
|||
|
int i = 0;
|
|||
|
for (int num = 0; num < m + 1; num++) {
|
|||
|
for (int j = 0; j < counter[num]; j++, i++) {
|
|||
|
nums[i] = num;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
// <20><><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A3ACB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void countingSort(vector<int> &nums) {
|
|||
|
// 1. ͳ<><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> m
|
|||
|
int m = 0;
|
|||
|
for (int num : nums) {
|
|||
|
m = max(m, num);
|
|||
|
}
|
|||
|
// 2. ͳ<>Ƹ<EFBFBD><C6B8><EFBFBD><EFBFBD>ֵij<D6B5><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
|
|||
|
// counter[num] <20><><EFBFBD><EFBFBD> num <20>ij<EFBFBD><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
|
|||
|
vector<int> counter(m + 1, 0);
|
|||
|
for (int num : nums) {
|
|||
|
counter[num]++;
|
|||
|
}
|
|||
|
// 3. <20><> counter <20><>ǰ<C7B0>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// <20><> counter[num]-1 <20><> num <20><> res <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>γ<EFBFBD><CEB3>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
for (int i = 0; i < m; i++) {
|
|||
|
counter[i + 1] += counter[i];
|
|||
|
}
|
|||
|
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> res
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> res <20><><EFBFBD>ڼ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>
|
|||
|
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; // <20><> num <20><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
counter[num]--; // <20><>ǰ<C7B0><D7BA><EFBFBD>Լ<EFBFBD> 1 <20><><EFBFBD>õ<EFBFBD><C3B5>´η<C2B4><CEB7><EFBFBD> num <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
// ʹ<>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> res <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD> nums
|
|||
|
nums = res;
|
|||
|
}
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
vector<int> nums = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
|
|||
|
countingSortNaive(nums);
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD> nums = ";
|
|||
|
printVector(nums);
|
|||
|
|
|||
|
vector<int> nums1 = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
|
|||
|
countingSort(nums1);
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD> nums1 = ";
|
|||
|
printVector(nums1);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|