Pagini recente » Cod sursa (job #1577258) | Cod sursa (job #397509) | Cod sursa (job #1384151) | Cod sursa (job #77640) | Cod sursa (job #1283683)
#include <iostream>
#include <fstream>
#include <vector>
#define FIN "algsort.in"
#define FOUT "algsort.out"
using namespace std;
typedef unsigned long ulong;
template <class T>
class Sort {
public:
typedef vector<T> Vector;
typedef void (*funcPtr)(Vector& vect);
static void QuickSort(Vector& vect) {
Sort::Helper::QuickSort(vect, 0, vect.size() - 1);
};
class Helper {
public:
static void QuickSort(Vector& vect, T left, T right) {
T i = left,
aux,
j = right,
pivot = vect[ (left + right) >> 1 ];
while( i <= j ) {
while(vect[ i ] < pivot) i++;
while(vect[ j ] > pivot) j--;
if( i <= j ) {
aux = vect[ i ] ^ vect[ j ];
vect[ i ] = aux ^ vect[ i ];
vect[ j ] = aux ^ vect[ j ];
i++; j--;
}
}
if(left < j) QuickSort(vect, left, j);
if(i < right) QuickSort(vect, i, right);
};
};
};
int main() {
int n;
ifstream fin( FIN );
ofstream fout( FOUT );
fin>>n;
vector<ulong> vect( n );
for(ulong i = 0; i < n; i++) fin>>vect[i];
Sort<ulong>::funcPtr sorter = Sort<ulong>::QuickSort;
sorter( vect );
for(ulong i = 0; i < n; i++) fout<<vect[i]<<" ";
fin.close();
fout.close();
return(0);
};