fork download
  1. #include <iostream>
  2. using namespace std;
  3. int count=0;
  4. void swap(int &v1,int &v2)
  5. {
  6. int t;
  7. t=v2;
  8. v2=v1;
  9. v1=t;
  10. }
  11.  
  12. void printArray(int arr[], int size) {
  13. for ( int i = 0; i < size; i++ ) {
  14. cout << arr[i] << ' ';
  15. }
  16. cout << endl;
  17. }
  18.  
  19. void partition(int data[], int size, int pivot_v, int& low,int& high) {
  20. low=-1;
  21. high=size;
  22. count++;
  23. for (int i=0;i<high;) {
  24. if(data[i]>pivot_v){
  25. swap(data[i], data[++low]);
  26. ++i;
  27. }
  28. else if(data[i]<pivot_v){
  29. swap(data[i], data[--high]);
  30. }
  31. else{++i;}
  32.  
  33. }
  34. cout<<"After Partiton NO:"<<count<<" low="<<low<<" high="<<high<<" array conetent:";
  35. printArray(data,size);
  36. }
  37.  
  38. void xsort(int data[],int size) {
  39. int pivot,low,high;
  40. if (size<=1)return;
  41. pivot=size/2;
  42. partition(data,size,data[pivot],low,high);
  43. xsort(data,low+1);
  44. xsort(data+high,size-high);
  45. }
  46.  
  47. int main(void) {
  48. int arr[]={2,0,1,3,2,4,1,0,0,1};
  49. xsort(arr,10);
  50. printArray(arr,10);
  51. cout<<"partiton count:"<<count;
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5260KB
stdin
Standard input is empty
stdout
After Partiton NO:1  low=-1  high=1 array conetent:4 1 3 2 0 1 0 0 1 2 
After Partiton NO:2  low=2  high=6 array conetent:3 2 2 1 1 1 0 0 0 
After Partiton NO:3  low=0  high=3 array conetent:3 2 2 
After Partiton NO:4  low=-1  high=3 array conetent:0 0 0 
4 3 2 2 1 1 1 0 0 0 
partiton count:4