Cod sursa(job #2292871)

Utilizator stan_flaviusStan Flavius Stefan stan_flavius Data 30 noiembrie 2018 10:10:53
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#define nmax 500001

using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");

int a[nmax],n;

void CountSort(int v[],int nr,int mod)
{ int viz[10]={0};
  int i;
  for(i=1; i<=nr; i++)
      viz[v[i]/mod%10]++;
  for(i=1; i<=9; i++)
      viz[i]+=viz[i-1];

  int aux[nmax];
  for(i=nr; i>=1; i--)
      { aux[viz[v[i]/mod%10]]=v[i];
        viz[v[i]/mod%10]--;
      }
  for(i=1; i<=nr; i++)
      v[i]=aux[i];
}

void RadixSort(int v[],int nr)
{ int mx=0;
  int i;
  for(i=1; i<=nr; i++)
      if(v[i]>mx) mx=v[i];
  int ct=0; ///nr maxim de cifre din vector
  while(mx!=0) {ct++; mx/=10;}
  int mod=1;
  for(i=1; i<=ct; i++)
      { CountSort(v,nr,mod);
        mod*=10;
      }

}

int main()
{ fin>>n;
  int i;
  for(i=1; i<=n; i++) fin>>a[i];
  RadixSort(a,n);
  for(i=1; i<=n; i++) fout<<a[i]<<" ";
    return 0;
}