Cod sursa(job #3333149)

Utilizator TonyyAntonie Danoiu Tonyy Data 11 ianuarie 2026 14:31:50
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
using namespace std;

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

vector<int> v;

void shellSort(vector<int>& v, int n) {
    stack<int> gaps({1, 4, 10, 23, 57, 132, 301});
    int gap = 701;
    while(gap < n) {
        gaps.push(gap);
        gap *= 2.25;
        if(gap & 1 == 0) {
            gap++;
        }
    }
    while(!gaps.empty()) {
        gap = gaps.top();
        gaps.pop();
        for(int i = gap; i < n; i++) {
            int x = v[i];
            int j = i;
            while(j >= gap && v[j - gap] > x) {
                v[j] = v[j - gap];
                j -= gap;
            }
            v[j] = x;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);

    int n;
    fin >> n;
    for(int i = 0, x; i < n; i++) {
        fin >> x;
        v.push_back(x);        
    }
    shellSort(v, n);
    for(const int& i: v) {
        fout << i << " ";
    }

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