fork(54) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void quickSort(vector<int>&,int,int);
  7.  
  8. int partition(vector<int>&, int,int);
  9.  
  10. int main()
  11. {
  12. vector<int> A = {6,10,13,5,8,3,2,25,4,11};
  13. int p=0;
  14. int q=10;
  15.  
  16. cout<<"======Original======="<<endl;
  17. for(auto e: A)
  18. cout<< e <<" ";
  19. cout<< endl;
  20.  
  21. quickSort(A,p,q);
  22.  
  23. cout<<"======Sorted======="<<endl;
  24. for(auto e: A)
  25. cout<< e <<" ";
  26. cout<< endl;
  27. }
  28.  
  29.  
  30. void quickSort(vector<int>& A, int p,int q)
  31. {
  32. int r;
  33. if(p<q)
  34. {
  35. r=partition(A, p,q);
  36. quickSort(A,p,r);
  37. quickSort(A,r+1,q);
  38. }
  39. }
  40.  
  41.  
  42. int partition(vector<int>& A, int p,int q)
  43. {
  44. int x= A[p];
  45. int i=p;
  46. int j;
  47.  
  48. for(j=p+1; j<q; j++)
  49. {
  50. if(A[j]<=x)
  51. {
  52. i=i+1;
  53. swap(A[i],A[j]);
  54. }
  55.  
  56. }
  57.  
  58. swap(A[i],A[p]);
  59. return i;
  60. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
======Original=======
6 10 13 5 8 3 2 25 4 11 
======Sorted=======
2 3 4 5 6 8 10 11 13 25