fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int partition (int a[], int l, int r) {
  5. int pivot = a[r];
  6. int i = l - 1;
  7. for (int j = l; j < r; j++) {
  8. if (a[j] < pivot) {
  9. i++;
  10. swap(a[i], a[j]);
  11. }
  12. }
  13. swap(a[i+1],a[r]);
  14. return i + 1;
  15. }
  16. void quickSort(int a[], int l, int r) {
  17. if (l < r) {
  18. int p = partition(a, l, r);
  19. quickSort(a, l, p - 1);
  20. quickSort(a, p + 1, r);
  21. }
  22. }
  23.  
  24. int main() {
  25. int n; cin >> n;
  26. int a[n];
  27. for (int i = 0; i < n; i++) cin >> a[i];
  28. quickSort(a, 0, n - 1);
  29. for (int i = 0; i < n; i++) cout << a[i] << " ";
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5316KB
stdin
7
5 8 3 2 4 7 6
stdout
2 3 4 5 6 7 8