Pagini recente » Cod sursa (job #2304633) | Cod sursa (job #2870739) | Cod sursa (job #1424053) | Cod sursa (job #3002992) | Cod sursa (job #1085496)
#include <fstream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <functional>
using namespace std;
int main()
{
ifstream cin("algsort.in");
ofstream cout("algsort.out");
/*
Shell sort
gap de forma 4^k + 2^(k - 1) + 1
prefixat cu 1
O(N^(4/3))
*/
vector<int> gaps;
for (int k = 9; k > 0; k--) {
gaps.push_back((1 << (k * 2)) + 3 * (1 << (k - 1)) + 1);
}
gaps.push_back(1);
int n;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (const int &gap : gaps) {
int i, j, temp;
for (i = gap; i < n; i++) {
temp = a[i];
for (j = i; j >= gap && a[j - gap] > temp; j -= gap) {
a[j] = a[j - gap];
}
a[j] = temp;
}
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}