Pagini recente » Cod sursa (job #2730613) | Cod sursa (job #471962) | Cod sursa (job #71037) | Cod sursa (job #3155125) | Cod sursa (job #1281195)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#define FIN "algsort.in"
#define FOUT "algsort.out"
using namespace std;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef long long ll;
template<class T>
class Sort {
public:
typedef vector<T> Vector;
typedef void (*FuncPtr)(Vector& vect);
typedef typename Vector::size_type Size;
static void ShellSort(Vector& vect) {
Sort::Helper::shellsort(vect, vect.size());
};
class Helper {
public:
static void shellsort(Vector& vect, int n) {
ll i,
j,
aux,
h;
do {
h = 3 * h + 1;
} while( h <= n);
do {
h /= 3;
for(j = h; j < n; j++) {
aux = vect[ j ];
for(i = j - h; i >= 0 && vect[ i ] > aux; i -= h) {
vect[ i + h ] = vect[ i ];
}
vect[ i + h ] = aux;
}
} while( h > 1);
}
};
};
int main() {
ifstream fin( FIN );
ofstream fout( FOUT );
ulong n,
elem;
fin>>n;
vector<ulong> vect(n);
for(ulong i = 0; i < n; i++) fin>>vect[i];
if(!fin || !fout) {
cerr<<"Error opening one of";
}
Sort<ulong>::FuncPtr sorter = Sort<ulong>::ShellSort;
sorter( vect );
ostream& target = fout;
copy(vect.begin(), vect.end(), ostream_iterator<ulong>(target," "));
return(0);
};