Cod sursa(job #3124406)

Utilizator daristyleBejan Darius-Ramon daristyle Data 28 aprilie 2023 16:12:21
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>

using namespace std;

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

const int N_MAX=5e5;
const int BIT_MAX=30;
int v[N_MAX];

void RadixSort(int v[], int begin, int end, int bit){//MSD
    if(end<=begin || bit<0)
        return;

    int b=begin, e=end;
    while(b<end && (v[b]&(1<<bit))==0)
        ++b;
    while(e>begin && (v[e]&(1<<bit))>0)
        --e;
    while(e-b>1){
        int aux=v[b];
        v[b]=v[e];
        v[e]=aux;
        do
            ++b;
        while(b<e && (v[b]&(1<<bit))==0);
        do
            --e;
        while(e>b && (v[e]&(1<<bit))>0);
    }

    b=begin;
    while(b<=end && (v[b]&(1<<bit))==0)
        ++b;

    RadixSort(v, begin, b-1, bit-1);
    RadixSort(v, b, end, bit-1);
}

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

    RadixSort(v,0, n-1, BIT_MAX);

    for(int i=0; i<n; ++i)
        fout<<v[i]<<' ';
    fout.put('\n');

    fin.close();
    fout.close();
    return 0;
}