Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <random>
- using namespace std;
- random_device rd;
- mt19937 gen(rd());
- template <typename It>
- It part(It first, It last, int x) {
- while (first != last && *first < x) ++first;
- if (first == last) return first;
- for (auto it = (first + 1); it != last; ++it){
- if (*it < x){
- iter_swap(it, first);
- ++first;
- }
- }
- return first;
- }
- template <typename It>
- void quick_sort(It first, It last) {
- if (last - first <= 1) return;
- auto pivot = *(first + gen() % (last - first));
- auto mid1 = part(first, last, pivot);
- auto mid2 = part(mid1, last, pivot + 1);
- quick_sort(first, mid1), quick_sort(mid2, last);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int n;
- cin >> n;
- vector<int> v(n);
- for (auto& x : v) cin >> x;
- quick_sort(v.begin(), v.end());
- for (const auto& x : v) cout << x << " ";
- cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment