Cod sursa(job #2203110)

Utilizator lonca.sorin1Lonca Sorin lonca.sorin1 Data 10 mai 2018 23:26:54
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("date.in");
ofstream g("date.out");

int getMax(int x[], int n)
{
    int mx = x[1];
    for (int i = 2; i <= n; i++)
        if (x[i] > mx)
            mx = x[i];
    return mx;
}

void countSort(int x[], int n, int exp)
{
    int b[n];
    int i, count[10] = {0};

    for (i = 1; i <= n; i++)
        count[(x[i] / exp) % 10]++;

    for (i = 1; i < 10; i++)
        count[i] += count[i - 1];

    for (i = n; i >= 1; i--)
    {
        b[count[(x[i] / exp) % 10]] = x[i];
        count[(x[i] / exp) % 10]--;
    }

    for (i = 1; i <= n; i++)
        x[i] = b[i];
}

void radixsort(int x[], int n)
{
    int m = getMax(x, n);
    for (int exp = 1; m / exp > 0; exp *= 10)
        countSort(x, n, exp);
}

int main()
{
    int n, x[110];
    f>>n;
    for (int i = 1; i <= n; ++i)
        f>>x[i];
    radixsort(x, n);
    for (int i = 1; i <= n; ++i)
        cout<<x[i]<<" ";
    return 0;
}