Cod sursa(job #2237319)

Utilizator CatincaBCatinca Balinisteanu CatincaB Data 1 septembrie 2018 16:29:31
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <bitset>
#include <vector>
///shell-sort
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
const int N = 500010;
int n,x[N];
bitset<N> gapes;
vector<int> aux;
void heap_down(int,int);
int main()
{
    f>>n;
    for(int i=0; i<n; i++)
        f>>x[i];
    for(int i=1; i<=n; i*=2)
        aux.push_back(i);
    while(aux.size())
    {
        for(auto it:aux)
            gapes[it]=1;
        while(aux.size()&&(3*aux.back()>n))
            aux.pop_back();
        if(aux.size())
            for(auto &it:aux)
                it*=3;
    }
    for(int k=n; k>=1; k--)
        if(gapes[k])
            for (int i = k; i < n; i++)
            {

                int temp = x[i],j;

                for (j = i; j >= k && x[j - k] > temp; j -= k)
                {
                    x[j] = x[j - k];
                }
                x[j] = temp;
            }

    for(int i=0; i<n; i++)
        g<<x[i]<<' ';
    return 0;
}