fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void quickSort(int arr[], int low, int high) {
  5. if (low < high) {
  6. int pivot = arr[high];
  7. int i = low - 1;
  8.  
  9. for (int j = low; j < high; j++) {
  10. if (arr[j] < pivot) {
  11. i++;
  12. // Swap arr[i] and arr[j]
  13. int temp = arr[i];
  14. arr[i] = arr[j];
  15. arr[j] = temp;
  16. }
  17. }
  18.  
  19. // Swap arr[i+1] and arr[high] (pivot)
  20. // This is where the pivot is placed in its correct sorted position
  21. int pi = i + 1;
  22. int temp = arr[pi];
  23. arr[pi] = arr[high];
  24. arr[high] = temp;
  25.  
  26. quickSort(arr, low, pi - 1);
  27. quickSort(arr, pi + 1, high);
  28. }
  29. }
  30.  
  31. int main() {
  32. int n;
  33. cin >> n;
  34. int arr[n];
  35. for (int i = 0; i < n; i++) {
  36. cin >> arr[i];
  37. }
  38. quickSort(arr, 0, n - 1);
  39. for (int i = 0; i < n; i++) {
  40. cout << arr[i] << " ";
  41. }
  42. cout << endl;
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5264KB
stdin
3
2 3 1

stdout
1 2 3