Pagini recente » Cod sursa (job #3152751) | Cod sursa (job #3185524) | Cod sursa (job #526418) | Cod sursa (job #1093859) | Cod sursa (job #949253)
Cod sursa(job #949253)
#include <chrono>
#include <random>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
using namespace std;
const int NMAX = 500011;
int v[NMAX];
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
inline void qsort(int left, int right)
{
if(left >= right) return;
if(right - left <= 2)
{
for(int i = left; i < right; ++i)
{
for(int j = i + 1; j <= right; ++j)
{
if(v[i] > v[j])
swap(v[i], v[j]);
}
}
return;
}
int i, j;
swap(v[left], v[gen() % (right - left) + left]);
i = right + 1;
j = left;
while(true)
{
while(v[--i] > v[left]);
while(j < right && v[++j] < v[left]);
if(j >= i) break;
swap(v[i], v[j]);
}
swap(v[left], v[j]);
qsort(left, j - 1);
qsort(j + 1, right);
}
int main()
{
int N;
ifstream in("algsort.in");
ofstream out("algsort.out");
in >> N;
copy(istream_iterator<int>(in), istream_iterator<int>(), v + 1);
qsort(1, N);
copy(v + 1, v + N + 1, ostream_iterator<int>(out, " "));
out << '\n';
return EXIT_SUCCESS;
}