Pagini recente » Cod sursa (job #2434537) | Cod sursa (job #219506) | Cod sursa (job #1555489) | Cod sursa (job #2121362) | Cod sursa (job #1241202)
#include <fstream>
#include <iterator>
#include <random>
#include <utility>
#include <vector>
using namespace std;
struct Generator {
mt19937 gen;
Generator() : gen() {}
int operator()(const int& a, const int& b) {
return uniform_int_distribution<int>(a, b)(gen);
}
} uniform;
/*
inline pair<int, int> partition(
const int& left,
const int& right,
std::vector<int>& V) {
// random pivot;
swap(V[right], V[ uniform(left, right) ]);
int pivot = left;
for (int i = left; i < right; ++i)
if (V[i] < V[right])
swap(V[i], V[pivot++]);
swap(V[right], V[pivot]);
return make_pair(pivot, pivot);
}
*/
inline pair<int, int> partition(
const int& left,
const int& right,
std::vector<int>& V) {
// random pivot;
swap(V[left], V[ uniform(left, right) ]);
int pivot_l = left, pivot_r = left;
for (int i = left + 1; i <= right; ++i)
if (V[i] == V[pivot_l]) {
swap(V[i], V[++pivot_r]);
}
else if (V[i] < V[pivot_l]) {
int t = V[pivot_l];
V[pivot_l++] = V[i];
V[i] = V[++pivot_r];
V[pivot_r] = t;
}
return make_pair(pivot_l, pivot_r);
}
void quicksort(
const int& left,
const int& right,
std::vector<int>& V) {
if (left < right) {
auto pivot = partition(left, right, V);
quicksort(left, pivot.first - 1, V);
quicksort(pivot.second + 1, right, V);
}
}
int main() {
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int n; fin >> n;
vector<int> V;
copy(istream_iterator<int>(fin), istream_iterator<int>(), back_inserter(V));
quicksort(0, V.size() - 1, V);
copy(V.begin(), V.end(), ostream_iterator<int>(fout, " "));
fout << endl;
return 0;
}