Cod sursa(job #1010019)

Utilizator tudorv96Tudor Varan tudorv96 Data 14 octombrie 2013 09:48:44
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#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] << " ";
}