Pagini recente » Cod sursa (job #3348652) | Cod sursa (job #3322985) | Monitorul de evaluare | Borderou de evaluare (job #1517507) | Cod sursa (job #3333145)
#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 h = 701;
while(h < n) {
gaps.push(h);
h *= 2.25;
if(h % 2 == 0) {
h++;
}
}
while(!gaps.empty()) {
int 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;
}