Cod sursa(job #3199942)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 2 februarie 2024 22:35:21
Problema Sortare prin comparare Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
using pii = pair<int,int>;
ifstream cin("algsort.in");
ofstream cout("algsort.out");
const int nmax = 5e5 + 1;
const int buc = 707;
vector <int> st[710];
int sz[710];
int n , a;
int main()
{
    cin >> n;
    for(int i = 1 ; i <= n ; ++i)
    {
        cin >> a;
        st[i/buc].push_back(a);
        sz[i/buc]++;
    }
    int mb = n/buc;
    for(int i = 0 ; i <= mb ; ++i)
    {
        bool ok = 0;
        while(!ok)
        {
            ok = 1;
            for(int j = 0 ; j < sz[i]-1 ; ++j)
            {
                if(st[i][j] < st[i][j+1])
                {
                    swap(st[i][j],st[i][j+1]);
                    ok = 0;
                }
            }
        }
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        int mn = 1e9, mp;
        for(int j = 0 ; j <= mb ; ++j)
        {
            if(sz[j] && st[j][sz[j]-1] < mn)
            {
                mn = st[j][sz[j]-1];
                mp = j;
            }
        }
        cout << mn << ' ';
        sz[mp]--;
    }
    return 0;
}