三数取中:
static int GetLow(int *arr,int low,int high)
{
int mid = low+( ( high - low ) >>1 ) ;
int tmp ;
if( arr[mid] > arr[high] )
{
tmp = arr[mid] ;
arr[mid] = arr[high] ;
arr[high] = tmp ;
}
if( arr[mid] > arr[low] )
{
tmp = arr[mid] ;
arr[mid] = arr[low] ;
arr[low] = tmp ;
}
if( arr[low] > arr[high] )
{
tmp = arr[low] ;
arr[low] = arr[high] ;
arr[high] = tmp ;
}
return arr[low] ;
}
聚集优化:
static int Partition(int *arr,int low,int high,int *leftlen,int *rightlen)
{
int chtmp ;
int tmp ;
int first = low ;
int last = high ;
int left = low ;
int right = high ;
*leftlen = 0 ;
*rightlen = 0 ;
tmp = GetLow(arr,low,high) ;
while( low < high )
{
while( low < high && tmp <= arr[high] )
{
if( tmp == arr[high] )
{
chtmp = arr[high] ;
arr[high] = arr[right] ;
arr[right] = chtmp ;
++(*rightlen) ;
right-- ;
}
high-- ;
}
arr[low] = arr[high] ;
while( low < high && tmp >= arr[low] )
{
if( tmp == arr[low] )
{
chtmp = arr[low] ;
arr[low] = arr[left] ;
arr[left] = chtmp ;
++(*leftlen) ;
left++ ;
}
low++ ;
}
arr[high] = arr[low] ;
}
arr[low] = tmp ;
int i = first ;
int j = low - 1 ;
while( i < left && arr[j] != tmp )
{
chtmp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = chtmp ;
i++ ;
j-- ;
}
i = last ;
j = high+1 ;
while( i > right && arr[j] != tmp )
{
chtmp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = chtmp ;
i-- ;
j++ ;
}
return low ;
}
小数用插排:
static void Quick(int *arr,int low,int high)
{
if(low >= high)
return ;
if(high-low < 20)
{
InsertSort(arr+low,high-low+1);
return ;
}
int leftlen ;
int rightlen ;
int parlow = Partition(arr,low,high,&leftlen,&rightlen) ;
if( parlow-1-leftlen > low )
Quick(arr,low,parlow-1-leftlen) ;
if( parlow +1+rightlen < high )
Quick(arr,parlow+1+rightlen,high) ;
}