fork download
  1. #include<iostream>
  2.  
  3. #include<time.h>
  4. using namespace std;
  5.  
  6. int partition (int *a, int s, int e)
  7. {
  8. int i=s;
  9. int j=e-1;
  10. int pivot = a[e],temp;
  11. while (i<=j)
  12. {
  13. if (a[i] < pivot)
  14. {
  15. i++;
  16. continue;
  17. }
  18. if (a[j] > pivot)
  19. {
  20. j--;
  21. continue;
  22. }
  23.  
  24. temp = a[i];
  25. a[i] = a[j];
  26. a[j] = temp;
  27. i++;
  28. j--;
  29. }
  30.  
  31. temp = a[i];
  32. a[i] = a[e];
  33. a[e] = temp;
  34. return i;
  35. }
  36.  
  37.  
  38.  
  39.  
  40. void qsort (int *a, int s, int e)
  41. {
  42. if (s>=e)
  43. {
  44. return;
  45. }
  46. int i = partition (a,s,e);
  47. qsort(a,s,i-1);
  48. qsort(a,i,e);
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54. int a[100];
  55. int i,j;
  56. int n = sizeof(a)/sizeof(a[0]);
  57. srand(time(NULL));
  58. for (j=0;j<n;j++)
  59. {
  60. a[j] = rand()%40;
  61. cout<<a[j]<<" ";
  62. }
  63. cout<<endl;
  64. clock_t start = clock();
  65. qsort(a,0,n-1);
  66.  
  67.  
  68. for (i=0;i<n;i++)
  69. {
  70. cout<<a[i]<<" ";
  71. }
  72. cout<<"\nTime elapsed: "<<((double)clock() - start) / CLOCKS_PER_SEC<<endl;
  73. }
  74.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
22 28 30 33 15 30 0 5 36 22 4 20 35 32 33 8 10 27 18 20 9 4 31 26 15 7 14 26 9 10 37 32 30 28 17 5 10 9 2 39 23 39 11 10 31 4 19 33 23 37 13 33 33 4 19 8 3 25 34 13 27 24 37 17 4 6 15 14 15 17 13 30 8 24 0 0 20 19 25 4 9 39 37 34 35 8 3 39 33 29 4 20 13 33 29 9 39 4 24 6 
0  0  0  2  3  3  4  4  4  4  4  4  4  4  5  5  6  6  7  8  8  8  8  9  9  9  9  9  10  10  10  10  11  13  13  13  13  14  14  15  15  15  15  17  17  17  18  19  19  19  20  20  20  20  22  22  23  23  24  24  24  25  25  26  26  27  27  28  28  29  29  30  30  30  30  31  31  32  32  33  33  33  33  33  33  33  34  34  35  35  36  37  37  37  37  39  39  39  39  39  
Time elapsed: 0