Cod sursa(job #3333147)

Utilizator TonyyAntonie Danoiu Tonyy Data 11 ianuarie 2026 14:27:17
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 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) {
    for(int gap = n >> 1; gap >= 1; gap >>= 1) {
        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;
}