achulkov2

Untitled

Mar 10th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4.  
  5. using namespace std;
  6.  
  7. random_device rd;
  8. mt19937 gen(rd());
  9.  
  10. template <typename It>
  11. It part(It first, It last, int x) {
  12. while (first != last && *first < x) ++first;
  13. if (first == last) return first;
  14. for (auto it = (first + 1); it != last; ++it){
  15. if (*it < x){
  16. iter_swap(it, first);
  17. ++first;
  18. }
  19. }
  20. return first;
  21. }
  22.  
  23. template <typename It>
  24. void quick_sort(It first, It last) {
  25. if (last - first <= 1) return;
  26. auto pivot = *(first + gen() % (last - first));
  27. auto mid1 = part(first, last, pivot);
  28. auto mid2 = part(mid1, last, pivot + 1);
  29. quick_sort(first, mid1), quick_sort(mid2, last);
  30. }
  31.  
  32. int main() {
  33. ios_base::sync_with_stdio(false);
  34. cin.tie(0);
  35. int n;
  36. cin >> n;
  37. vector<int> v(n);
  38. for (auto& x : v) cin >> x;
  39. quick_sort(v.begin(), v.end());
  40. for (const auto& x : v) cout << x << " ";
  41. cout << "\n";
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment