fork(2) download
  1. //ABcDexter
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int count=0; //count function calls
  6.  
  7. int MedianOf3P(int a[],int p, int r)
  8. {
  9. int x=a[p],y=a[(r-p)/2+p],z=a[r-1],i=p-1,j=r;
  10. if (y>x && y<z || y>z && y<x ) x=y;
  11. else if (z>x && z<y || z>y && z<x ) x=z;
  12. while (true)
  13. {
  14. do {j--;} while (a[j] > x);
  15. do {i++;} while (a[i] < x);
  16. if (i < j) std::swap(a[i],a[j]);
  17. else return j+1;
  18. }
  19. }
  20. void QuickSort_Median(int a[],int start,int end)
  21. {
  22. int pivot;
  23. if (end-start<2) return;
  24. pivot=MedianOf3P(a,start,end);
  25. ::count++; /*function call*/ QuickSort_Median(a,start,pivot);
  26. ::count++; /*function call*/ QuickSort_Median(a,pivot,end);
  27. }
  28.  
  29. int i,n,t;
  30. int main(){
  31. n=15; int A[n];
  32. for(i=0,t=1048576;i<n;i++)
  33. { A[i]=(rand()%t);
  34. }
  35. for(i=0;i<n;i++)
  36. cout<<A[i]<<",";
  37.  
  38. QuickSort_Median(A,0,n); cout<<"\nsorted:\n";
  39. for(i=0;i<n;i++)
  40. cout<<A[i]<<",";
  41. cout<<"Ans : "<<::count<<endl;
  42.  
  43. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
738663,730054,825449,215155,56401,613631,562250,350444,925481,556237,743610,972715,82418,466683,174563,
sorted:
56401,82418,174563,215155,350444,466683,556237,562250,613631,730054,738663,743610,825449,925481,972715,Ans : 28