Pagini recente » Cod sursa (job #2027542) | Cod sursa (job #1018440) | Cod sursa (job #2413048) | Cod sursa (job #1910436) | Cod sursa (job #1010019)
#include <fstream>
using namespace std;
const int N = 5e5 + 5;
ifstream fin ("algsort.in");
ofstream fout ("algsort.out");
int n, v[N], w[N], c[10], MAX = -1, imp = 0;
char last[N];
void Sort(int x) { //Sortez dupa a lg(zec) cifra
char last[N];
for (int i = 1; i <= n; ++i) {
int aux = v[i];
for (int j = 1; j <= x; ++j)
aux /= 10;
last[i] = aux % 10;
}
for (int i = 0; i <= 9; ++i)
c[i] = 0;
for (int i = 1; i <= n; ++i)
c[(int)last[i]]++;
for (int i = 1; i <= 9; ++i)
c[i] += c[i-1];
for (int i = n; i; --i) {
w[c[(int)last[i]]] = v[i];
c[(int)last[i]]--;
}
}
int main() {
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> v[i];
MAX = max (v[i], MAX);
}
int nrcif = 0;
while (MAX) {
nrcif++;
MAX /= 10;
}
for (int i = 0; i < nrcif; ++i) {
Sort (i);
for (int j = 1; j <= n; ++j)
v[j] = w[j];
}
for (int i = 1; i <= n; ++i)
fout << w[i] << " ";
}