算法笔记 --- Radix Sort

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;

class RadixSort {
public:
    int* radixSort(int* A, int n) {
        // write code here
        //because value is smaller than 2000, so => 1000
        queue<int> bucket[10];    
        int d, index, k, p = 3;
        for(int i = 0; i <= p; i++){
            d = pow(10, i);
            for(int j = 0; j < n; j++){
                index = (A[j] / d) % 10;
            
                bucket[index].push(A[j]);
            }
            k = 0;
            for(int i = 0; i < 10; i++){
                while(!bucket[i].empty()){
                    A[k++] = bucket[i].front();
                    bucket[i].pop();
                }
            }
        }
        return A;
    }
};
int main()
{
    int a[13] = {54,35,48,36,27,12,44,44,8,14,26,17,28};
    int* res;
    RadixSort sorter;
    res = sorter.radixSort(a, 13);
    cout<<"after sorting:"<<endl;
    for(int i = 0; i < 13; i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
   
    return 0;
}
原文地址:https://www.cnblogs.com/zhongzhiqiang/p/5791091.html