fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int ctr=0;
  5. void swap(int &a,int &b)
  6. {
  7. ctr=ctr+1;
  8. int temp = a;
  9. a=b;
  10. b = temp;
  11. }
  12. int partition (int A[], int p, int r)
  13. {
  14. int x = A[r];
  15. int i = p - 1;
  16.  
  17. for (int j = p; j <= r- 1; j++)
  18. {
  19. if (A[j] <= x)
  20. {
  21. i++;
  22. swap (A[i], A[j]);
  23. }
  24. }
  25. swap (A[i + 1], A[r]);
  26. return (i + 1);
  27. }
  28.  
  29. void quickSort(int A[], int p, int r)
  30. {
  31. if (p < r)
  32. {
  33. int q = partition(A, p,r);
  34. quickSort(A, p, q - 1);
  35. quickSort(A, q + 1, r);
  36. }
  37. }
  38. int main()
  39. {
  40.  
  41. int a[] = {82,49,26,1,66,47};
  42. int n = sizeof(a)/sizeof(a[0]);
  43. quickSort(a,0,n-1);
  44. cout<<ctr;
  45. }
Success #stdin #stdout 0.01s 5404KB
stdin
Standard input is empty
stdout
9