Cod sursa(job #1734113)

Utilizator codebreaker24Tivadar Ionut codebreaker24 Data 26 iulie 2016 15:25:38
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>

using namespace std;
const int nmax = 10000001;
int a[nmax];
int temp[nmax];
int counts[257];
int *ap, *tempp, *aux;
int n;
int A,B,C;
int i;

    ifstream fin("algsort.in");
    ofstream fout("algsort.out");
void radixSort();

int main()
{


    fin >> n;


    for(int i=0; i<n; i++)
    {
       fin >> a[i];


    }

    ap = a;
    tempp = temp;


    radixSort();

    for(int i=0; i<n; i++)
    {

        fout << ap[i] << " ";
    }
    fout << '\n';



    fin.close();
    fout.close();



    return 0;
}

void radixSort()
{

    for(int digit = 0;  digit <4;  digit++)
    {
            for( i=0;i<256; i++)
                counts[i] =0;

            for(i=0; i<n; i++)
               counts[(ap[i]>>(digit*8))&255]++;


            for(i=1; i<256; i++)
                counts[i] +=counts[i-1];

            for(i=n-1; i>=0; i--)
            {
                tempp[counts[(ap[i]>>(digit*8))&255]-1] = ap[i];
                counts[(ap[i]>>(digit*8))&255]--;
            }


            aux = tempp;
            tempp = ap;
            ap = aux;


    }



}