Advertisement
rotno98

Quick_sort corman

Oct 15th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int Partition(int* A,int p,int r)
  7. {
  8. int x=A[r];
  9. int i=p-1;
  10.  
  11. for(int j=p;j<r-1;j++)
  12. {
  13. if(A[j]<=x)
  14. {
  15. i=i+1;
  16. swap(A[i],A[j]);
  17.  
  18. cout<<"x"<<endl;
  19. }
  20. }
  21.  
  22. swap(A[i+1],A[r]);
  23. return i+1;
  24.  
  25. }
  26.  
  27. void Quick(int * A,int p,int r)
  28. {
  29. if(p<r)
  30. {
  31. int q=Partition(A,p,r);
  32. Quick(A,p,q-1);
  33. Quick(A,q+1,r);
  34. }
  35. }
  36.  
  37.  
  38.  
  39. int main()
  40. {
  41. int A[]={20,30,10,40,50,5,100,80};
  42.  
  43. int z=sizeof(A)/sizeof(A[0]);
  44.  
  45.  
  46.  
  47. for(int i=0;i<z;i++)
  48. cout<<A[i]<<",";
  49.  
  50. cout<<endl;
  51.  
  52. //cout<<z<<endl;
  53.  
  54. Quick(A,0,z-1);
  55.  
  56. cout<<z<<endl;
  57.  
  58. for(int i=0;i<z;i++)
  59. cout<<A[i]<<",";
  60.  
  61. cout<<endl;
  62.  
  63. //cout<<sizeof(A)/sizeof(A[0]);
  64.  
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement