Pagini recente » Cod sursa (job #2713853) | Cod sursa (job #1024826) | Cod sursa (job #2074947) | Cod sursa (job #1403743) | Cod sursa (job #796968)
Cod sursa(job #796968)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
typedef long ulong;
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;
}