[模板]基数排序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
#define reg register
inline int read() {
    int res = 0;char ch=getchar();bool fu=0;
    while(!isdigit(ch))fu|=(ch=='-'),ch=getchar();
    while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
    return fu?-res:res;
}

int n;
int a[100005], tmp[100005];

inline void Bsort() 
{
    int mx = 0;
    for (reg int i = 1 ; i <= n ; i ++) mx = max(mx, a[i]);
    int dig = 0;
    while(mx) mx /= 10, dig++;
    int base = 1;
    while(dig--)
    {
        int buk[10] = {0};
        for (reg int i = 1 ; i <= n ; i ++) buk[a[i]/base%10]++;
        for (reg int i = 0 ; i < 9 ; i ++) buk[i + 1] += buk[i];
        for (reg int i = n ; i >= 1 ; i --) tmp[buk[a[i]/base%10]--] = a[i];
        for (reg int i = 1 ; i <= n ; i ++) a[i] = tmp[i];
        base *= 10;
    }
}

int main()
{
    n = read();
    for (reg int i = 1 ; i <= n ; i ++) a[i] = read();
    Bsort();
    for (reg int i = 1 ; i <= n ; i ++) printf("%d ", a[i]);
    return 0;
}

 

posted @ 2018-10-24 14:33  zZhBr  阅读(212)  评论(0编辑  收藏  举报