Pagini recente » Cod sursa (job #512890) | Cod sursa (job #2563178) | Cod sursa (job #3163731) | Cod sursa (job #1062890) | Cod sursa (job #3003458)
#include <bits/stdc++.h>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int a[500005];
void countSortB10(int a[], int n, int exp){
map<int, int> mp;
for(int i = 1; i <= n; ++i){
mp[(a[i] / exp) % 10]++;
}
for(int i = 1; i < 10; ++i){
mp[i] += mp[i - 1];
}
int* res = new int[500005];
for(int i = n; i >= 1; --i){
res[mp[(a[i] / exp) % 10]] = a[i];
mp[(a[i] / exp) % 10]--;
}
for(int i = 1; i <= n; ++i){
a[i] = res[i];
}
if(res != NULL){
delete[] res;
res = NULL;
}
}
void radixSortB10(int a[], int n, int maxi){
for(int exp = 1; maxi / exp > 0; exp *= 10){
countSortB10(a, n, exp);
}
}
int main(){
int n, maxi = -1e9;
f >> n;
for(int i = 1; i <= n; ++i){
f >> a[i];
maxi = max(maxi, a[i]);
}
radixSortB10(a, n, maxi);
for(int i = 1; i <= n; ++i){
g << a[i] << " ";
}
return 0;
}