1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
void quicksort(vector<int> &v,int low, int high)
{
int middle = ( low + high ) / 2;
if( v[ middle ] < v[ low ] )
swap( v[ low ], v[ middle ] );
if( v[ high ] < v[ low ] )
swap( v[ low ], v[ high ] );
if( v[ high ] < v[ middle ] )
swap( v[ middle ], v[ high ] );
int pivot = v[middle];
swap( v[ middle ] , v[ high-1 ]);
int i,j;
for(i=low, j=high-1; ;)
{
while(v[i] < pivot){i++;}
while(pivot < v[j]){j--;}
if(i<j)
swap( v[ i ] , v[ j ] );
else
break;
}
swap( v[ i ], v[ high - 1 ] );
quicksort(v,low,i-1 );
quicksort(v,i + 1,high);
}
void quicksort(vector<int> &v)
{
quicksort(v,0,v.size()-1);
}
|