Cod sursa(job #3003458)

Utilizator AndreiKatsukiAndrei Dogarel AndreiKatsuki Data 15 martie 2023 19:02:50
Problema Sortare prin comparare Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;

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

int a[500005];

void countSortB10(int a[], int n, int exp){
    map<int, int> mp;
    for(int i = 1; i <= n; ++i){
        mp[(a[i] / exp) % 10]++;
    }
    for(int i = 1; i < 10; ++i){
        mp[i] += mp[i - 1];
    }
    int* res = new int[500005];
    for(int i = n; i >= 1; --i){
        res[mp[(a[i] / exp) % 10]] = a[i];
        mp[(a[i] / exp) % 10]--;
    }
    for(int i = 1; i <= n; ++i){
        a[i] = res[i];
    }
    if(res != NULL){
        delete[] res;
        res = NULL;
    }
}

void radixSortB10(int a[], int n, int maxi){
    for(int exp = 1; maxi / exp > 0; exp *= 10){
        countSortB10(a, n, exp);
    }
}

int main(){
    int n, maxi = -1e9;
    f >> n;
    for(int i = 1; i <= n; ++i){
        f >> a[i];
        maxi = max(maxi, a[i]);
    }
    radixSortB10(a, n, maxi);
    for(int i = 1; i <= n; ++i){
        g << a[i] << " ";
    }
    return 0;
}