E.cpp 674 B

12345678910111213141516171819202122232425262728293031323334
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int pp(vector<int> &a, int p, int q) {
  4. int x = a[q];
  5. int i = p - 1;
  6. for (int j = p; j <= q - 1; j++) {
  7. if (a[j] <= x) {
  8. i++;
  9. swap(a[i], a[j]);
  10. }
  11. }
  12. swap(a[i + 1], a[q]);
  13. return i + 1;
  14. }
  15. void quick_sort(vector<int> &a, int p, int q) {
  16. if (p >= q)return;
  17. int r = pp(a, p, q);
  18. quick_sort(a, p, r - 1);
  19. quick_sort(a, r + 1, q);
  20. }
  21. int main() {
  22. int n;
  23. cin >> n;
  24. vector<int> a(n);
  25. for (auto &e:a)cin >> e;
  26. quick_sort(a, 0, n - 1);
  27. for (auto e:a)cout << e << " ";
  28. cout << endl;
  29. return 0;
  30. }