fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int partition(int a[], int si, int ei){
  5. int x = a[si], count = 0;
  6.  
  7. for(int i = si + 1; i <= ei; i++){
  8. if(a[i] <= x)
  9. count++;
  10. }
  11.  
  12. int pivot = si + count;
  13. int temp = a[si];
  14. a[si] = a[pivot];
  15. a[pivot] = temp;
  16.  
  17. for(int i = si, j = ei; i <= pivot && j >= pivot;){
  18. if(a[i] <= x)
  19. i++;
  20. else if(a[j] > x)
  21. j--;
  22. else{
  23. int temp = a[i];
  24. a[i] = a[j];
  25. a[j] = temp;
  26. i++;
  27. j--;
  28. }
  29. }
  30.  
  31. return pivot;
  32. }
  33.  
  34. void quick_sort(int a[], int si, int ei){
  35. if(si >= ei)
  36. return;
  37.  
  38. int c = partition(a, si, ei);
  39.  
  40. quick_sort(a, si, c - 1);
  41. quick_sort(a, c + 1, ei);
  42. }
  43.  
  44. int main(){
  45. int n,i,k,m;
  46. clock_t time_req;
  47. time_req=clock();
  48. cout << "enter size of the array" << endl;
  49. cin >> n;
  50. int a[n];
  51. cout<< "enter elements" << endl;
  52. for(k = 0; k < n; k++) cin >> a[k];
  53.  
  54. quick_sort(a, 0, n-1);
  55. cout<< "the sorted array is" <<endl;
  56.  
  57. for(m=0;m<n;m++)
  58. cout<<a[m]<<" ";
  59. cout<<endl;
  60.  
  61. time_req=clock()-time_req;
  62. cout<<"the time taken is"<<endl;
  63. cout<<(float)time_req/CLOCKS_PER_SEC<<"seconds"<<endl;
  64.  
  65. cout<<"The bytes occupied is"<<endl;
  66. cout<<(sizeof(a[n])*n)+sizeof(n)+sizeof(i)+sizeof(k)+sizeof(m);
  67. }
Success #stdin #stdout 0.01s 5544KB
stdin
5
0 10 5 6 1
stdout
enter size of the array
enter elements
the sorted array is
0 1 5 6 10 
the time taken is
4.7e-05seconds
The bytes occupied is
36