#include <iostream>
using namespace std;
int count=0;
void swap(int &v1,int &v2)
{
int t;
t=v2;
v2=v1;
v1=t;
}
void printArray(int arr[], int size) {
for ( int i = 0; i < size; i++ ) {
cout << arr[i] << ' ';
}
cout << endl;
}
void partition(int data[], int size, int pivot_v, int& low,int& high) {
low=-1;
high=size;
count++;
for (int i=0;i<high;) {
if(data[i]>pivot_v){
swap(data[i], data[++low]);
++i;
}
else if(data[i]<pivot_v){
swap(data[i], data[--high]);
}
else{++i;}
}
cout<<"After Partiton NO:"<<count<<" low="<<low<<" high="<<high<<" array conetent:";
printArray(data,size);
}
void xsort(int data[],int size) {
int pivot,low,high;
if (size<=1)return;
pivot=size/2;
partition(data,size,data[pivot],low,high);
xsort(data,low+1);
xsort(data+high,size-high);
}
int main(void) {
int arr[]={2,0,1,3,2,4,1,0,0,1};
xsort(arr,10);
printArray(arr,10);
cout<<"partiton count:"<<count;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgY291bnQ9MDsKdm9pZCBzd2FwKGludCAmdjEsaW50ICZ2MikKewoJaW50IHQ7Cgl0PXYyOwoJdjI9djE7Cgl2MT10Owp9Cgp2b2lkIHByaW50QXJyYXkoaW50IGFycltdLCBpbnQgc2l6ZSkgewogICAgZm9yICggaW50IGkgPSAwOyBpIDwgc2l6ZTsgaSsrICkgewogICAgICAgIGNvdXQgPDwgYXJyW2ldIDw8ICcgJzsKICAgIH0KICAgIGNvdXQgPDwgZW5kbDsKfQoKdm9pZCBwYXJ0aXRpb24oaW50IGRhdGFbXSwgaW50IHNpemUsIGludCBwaXZvdF92LCBpbnQmIGxvdyxpbnQmIGhpZ2gpIHsKCWxvdz0tMTsKCWhpZ2g9c2l6ZTsKCWNvdW50Kys7Cglmb3IgKGludCBpPTA7aTxoaWdoOykgewoJCWlmKGRhdGFbaV0+cGl2b3Rfdil7CgkJCXN3YXAoZGF0YVtpXSwgZGF0YVsrK2xvd10pOwoJCQ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