Pagini recente » Cod sursa (job #3160341) | Cod sursa (job #315997) | Cod sursa (job #655619) | Cod sursa (job #2148458) | Cod sursa (job #796969)
Cod sursa(job #796969)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
template<typename T>
class QSortParitionPredicate
{
public:
QSortParitionPredicate(const T& v) :
val(v)
{}
bool operator () (const T& elem) const
{
return elem < val;
}
private:
T val;
};
template <class Iter>
void quicksort(Iter start, Iter end)
{
if (std::distance(start, end) > 1)
{
Iter pivot = start + (end - start)/2;
QSortParitionPredicate<ulong> predQS(*pivot);
Iter partIndex = partition(start, end, predQS);
quicksort(start, partIndex);
quicksort(partIndex+1, end);
}
}
int main()
{
int n;
vector<ulong> vec;
fstream fin("algsort.in", fstream::in);
fstream fout("algsort.out", fstream::out);
fin >> n;
//cout << n << endl;
vec.reserve(n);
istream_iterator<ulong> inIt(fin);
istream_iterator<ulong> eofIt;
copy(inIt, eofIt, back_inserter(vec));
/*for (int i=0; i<n; ++i)
{
//fin >> vec[i];
cout << vec[i] << " ";
}
cout << endl;*/
quicksort(vec.begin(), vec.end());
ostream_iterator<ulong> outIt(fout, " ");
copy(vec.begin(), vec.end(), outIt);
//cout << endl;
return 0;
}