fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void swap(int *a,int *b)
  5. {
  6. int temp = *a;
  7. *a = *b;
  8. *b = temp;
  9. }
  10.  
  11. int partition (int *arr, int low, int high)
  12. {
  13. int pivot = arr[high]; // pivot
  14. int i = (low - 1); // Index of smaller element
  15.  
  16. for (int j = low; j <= high- 1; j++)
  17. {
  18. // If current element is smaller than or
  19. // equal to pivot
  20. if (arr[j] <= pivot)
  21. {
  22. // i++; // increment index of smaller element
  23. swap(&arr[++i], &arr[j]);
  24. }
  25. }
  26. swap(&arr[++i], &arr[high]);
  27. return (i);
  28. }
  29. void recQuicksort(int *arr,int l,int h)
  30. {
  31. if(l<h)
  32. {
  33. int pivot = partition(arr,l,h);
  34. recQuicksort(arr,l,pivot-1);
  35. recQuicksort(arr,pivot+1,h);
  36. }
  37. }
  38. int main() {
  39. // your code goes here
  40. int a[6] = {44, 33, 23, 22, 2, 1};
  41. recQuicksort(a, 0, 5);
  42. for (int i = 0; i < 6; i++) printf("%d ", a[i]);
  43. printf("\n");
  44. return 0;
  45. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
1 2 22 23 33 44