Pagini recente » Cod sursa (job #914429) | Cod sursa (job #331209) | Cod sursa (job #80950) | Cod sursa (job #243696) | Cod sursa (job #796974)
Cod sursa(job #796974)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <cstdlib>
using namespace std;
typedef unsigned int uint32;
void do_partition(vector<uint32>& vec, int &start, int &end, uint32 pivotValue)
{
while (start <= end)
{
while (vec[start] < pivotValue) start++;
while (pivotValue < vec[end]) end--;
if (start <= end)
{
swap(vec[start], vec[end]);
start++;
end--;
}
}
}
void quicksort(vector<uint32>& vec, int start, int end)
{
if (start < end)
{
int new_start = start;
int new_end = end;
int pivot = start + rand()%(end-start);
uint32 pivotValue = vec[pivot];
do_partition(vec, new_start, new_end, pivotValue);
quicksort(vec, start, new_end);
quicksort(vec, new_start, end);
}
}
int main()
{
int n;
vector<uint32> vec;
fstream fin("algsort.in", fstream::in);
fstream fout("algsort.out", fstream::out);
fin >> n;
vec.reserve(n);
istream_iterator<uint32> inIt(fin);
istream_iterator<uint32> eofIt;
copy(inIt, eofIt, back_inserter(vec));
quicksort(vec, 0, n-1);
ostream_iterator<uint32> outIt(fout, " ");
copy(vec.begin(), vec.end(), outIt);
return 0;
}