quicksort [template版]

inline Type Min(Type a, Type b) { 
        return a>b ? b : a; 
} 

template<typename Type> 
inline void Swap(Type** array, int x, int y) { 
        Type temp1;
		Type temp2;
        temp1=array[x][0]; 
		temp2=array[x][1]; 
        array[x][0]=array[y][0]; 
		array[x][1]=array[y][1]; 
        array[y][0]=temp1;
		array[y][1]=temp2;
} 

// quick sort 
template<typename Type> 
void qsort(Type** array, int low, int high){ 
        if(low<high){ 
        int lo=low; 
        int hi=high+1; 
        Type elem=array[lo][1];

        for(;;){ 
                while(Min(elem, array[++lo][1])!=elem && lo < high ); 
                        while(Min(elem, array[--hi][1])==elem && low < hi ); 
                        if(lo<hi) 
                                Swap(array, lo, hi); 
                        else 
                                break; 
        } 

        Swap(array,low,hi); 
        qsort(array,low, hi-1); 
        qsort(array,hi+1, high); 
        } 
}

你可能感兴趣的:(Quicksort)