Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- int getByte(int x, int num) {
- for (int i = 0; i < 3 - num; i++) {
- x /= 256;
- }
- return (x & 0x000000FF);
- }
- void countingSort(int *arr, int n, int num_byte) {
- const size_t size = 256;
- int cnt[size];
- for (int &i : cnt) {
- i = 0;
- }
- int res[n];
- memcpy(res, arr, sizeof(int) * n);
- for (int i = 0; i < n; ++i) {
- ++cnt[getByte(arr[i], num_byte)];
- }
- for (int i = 1; i < size; ++i) {
- cnt[i] += cnt[i - 1];
- }
- for (int i = n - 1; i >= 0; --i) {
- res[cnt[getByte(arr[i], num_byte)] - 1] = arr[i];
- --cnt[getByte(arr[i], num_byte)];
- }
- memcpy(arr, res, sizeof(int) * n);
- }
- void radixSort(int *arr, int n) {
- for (int i = 3; i > 0; i--) {
- countingSort(arr, n, i);
- }
- }
- int main() {
- int n = 0;
- std::cin >> n;
- int a[n];
- for (int i = 0; i < n; ++i) {
- std::cin >> a[i];
- }
- radixSort(a, n);
- for (int i = 0; i < n; ++i) {
- std::cout << a[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement