Pagini recente » Cod sursa (job #468899) | Cod sursa (job #521236) | Cod sursa (job #859609) | Cod sursa (job #1290471) | Cod sursa (job #1317587)
#include <iostream>
#include<fstream>
using namespace std;
#define B 13
#define MX 500002
struct List_vect
{
int urm[MX], first[B], last[B], size[B] ;
};
int a[MX], i,n ;
List_vect LstPrec, LstC;
void push_back(List_vect &L, int ind, int c )
{
if (L.size[c] > 0 )
L.urm[L.last[c] ]= ind; // pastrez legatura cu urmatorul element din lista
else L.first[c]=ind;
L.last[c]= ind;
L.size[c]++;
}
int pop_front(List_vect &L, int c) // c - cifra
{
int i= L.first[c];
L.first[c]=L.urm[L.first[c] ]; // schimb capul cozii
L.size[c]--;
return i;
}
void Clear(List_vect &L )
{
for (int i=0; i<10; i++)
L.first[i]=L.last[i]=L.size[i]= 0;
}
void Copy(List_vect &L1, List_vect &L2 )
{
int i;
for (i=0; i<10; i++)
{
L1.first[i]=L2.first[i];
L1.last[i]=L2.last[i];
L1.size[i]=L2.size[i];
}
for (i=0; i<=n; i++)
L1.urm[i]=L2.urm[i];
}
ifstream f1("algsort.in");
ofstream f2("algsort.out");
void radix_s()
{
int i,p=100 ;
for (i=1; i<=n; i++)
push_back(LstPrec, i, a[i] % 10 ); // pun nr in lista precedenta dupa ultima cifra
while (LstPrec.size[0] < n ) // cat nu au fost incluse toate in cutia 0
{
Clear(LstC);
for (i=0; i<=9; i++)
while (LstPrec.size[i] > 0 )
{
int v= pop_front(LstPrec, i );
push_back(LstC, v, (a[v]%p)/(p/10) ); // mutam varful din lista precedenta in cea curenta
} // conform cifrei log10 (p)-1
Copy(LstPrec, LstC); // copie o lista in alta
p*=10;
}
}
int main()
{
f1>>n;
for (i=1;i<=n; i++)
f1>>a[i];
radix_s();
for (i=1; i<=n; i++)
f2<<a[ pop_front(LstPrec,0 ) ]<<" ";
f2.close();
return 0;
}