cpp-algo-cases/chapter_sorting/radix_sort.cpp

66 lines
1.9 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: radix_sort.cpp
* Created Time: 2023-03-26
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><>ȡԪ<C8A1><D4AA> num <20>ĵ<EFBFBD> k λ<><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> exp = 10^(k-1) */
int digit(int num, int exp) {
// <20><><EFBFBD><EFBFBD> exp <20><><EFBFBD><EFBFBD> k <20><><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>ظ<EFBFBD>ִ<EFBFBD>а<EFBFBD><D0B0><EFBFBD><EFBFBD>Ĵη<C4B4><CEB7><EFBFBD><EFBFBD><EFBFBD>
return (num / exp) % 10;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣨸<EFBFBD><F2A3A8B8><EFBFBD> nums <20><> k λ<><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void countingSortDigit(vector<int> &nums, int exp) {
// ʮ<><CAAE><EFBFBD>Ƶ<EFBFBD>λ<EFBFBD><CEBB>ΧΪ 0~9 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>Ϊ 10 <20><>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD>
vector<int> counter(10, 0);
int n = nums.size();
// ͳ<><CDB3> 0~9 <20><><EFBFBD><EFBFBD><EFBFBD>ֵij<D6B5><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
for (int i = 0; i < n; i++) {
int d = digit(nums[i], exp); // <20><>ȡ nums[i] <20><> k λ<><CEBB><EFBFBD><EFBFBD>Ϊ d
counter[d]++; // ͳ<><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> d <20>ij<EFBFBD><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
}
// <20><>ǰ׺<C7B0>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 1; i < 10; i++) {
counter[i] += counter[i - 1];
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0>ͳ<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> res
vector<int> res(n, 0);
for (int i = n - 1; i >= 0; i--) {
int d = digit(nums[i], exp);
int j = counter[d] - 1; // <20><>ȡ d <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> j
res[j] = nums[i]; // <20><><EFBFBD><EFBFBD>ǰԪ<C7B0><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> j
counter[d]--; // <20><> d <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
}
// ʹ<>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD> nums
for (int i = 0; i < n; i++)
nums[i] = res[i];
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void radixSort(vector<int> &nums) {
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
int m = *max_element(nums.begin(), nums.end());
// <20><><EFBFBD>մӵ<D5B4>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int exp = 1; exp <= m; exp *= 10)
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>صĵ<D8B5> k λִ<CEBB>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// <20><> exp = 10^(k-1)
countingSortDigit(nums, exp);
}
/* Driver Code */
int main() {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<int> nums = {10546151, 35663510, 42865989, 34862445, 81883077,
88906420, 72429244, 30524779, 82060337, 63832996};
radixSort(nums);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD> nums = ";
printVector(nums);
return 0;
}