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