fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void swap(int& a, int& b) {
  5. int temp = a;
  6. a = b;
  7. b = temp;
  8. }
  9.  
  10. void printArray(int arr[], int size) {
  11. for (int i = 0; i < size; i++) {
  12. cout << arr[i] << " ";
  13. }
  14. cout << endl;
  15. }
  16.  
  17. int partition(int arr[], int low, int high, int step) {
  18. int pivot = arr[high];
  19. int i = low - 1;
  20.  
  21. for (int j = low; j < high; j++) {
  22. if (arr[j] < pivot) {
  23. i++;
  24. swap(arr[i], arr[j]);
  25. }
  26. }
  27. swap(arr[i + 1], arr[high]);
  28.  
  29. cout << "Buoc " << step << ": ";
  30. printArray(arr, high + 1);
  31.  
  32. return i + 1;
  33. }
  34.  
  35. void quickSort(int arr[], int low, int high, int& step) {
  36. if (low < high) {
  37. int pi = partition(arr, low, high, step);
  38. step++;
  39. quickSort(arr, low, pi - 1, step);
  40. quickSort(arr, pi + 1, high, step);
  41. }
  42. }
  43.  
  44. int main() {
  45. int n;
  46. cin >> n;
  47. int arr[n];
  48. for (int i = 0; i < n; i++) {
  49. cin >> arr[i];
  50. }
  51.  
  52. int step = 0;
  53. quickSort(arr, 0, n - 1, step);
  54.  
  55. cout << "Sorted array: \n";
  56. printArray(arr, n);
  57.  
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0.01s 5272KB
stdin
5
9 8 2 5 4
stdout
Buoc 0: 2 4 9 5 8 
Buoc 1: 2 4 5 8 9 
Sorted array: 
2 4 5 8 9