33 #ifndef _H_CUSTOM_QUICKSORT
34 #define _H_CUSTOM_QUICKSORT
50 template<
typename KeyType >
51 size_t custom_quicksort_getpivot(
const std::vector< KeyType > &key,
const size_t left,
const size_t right ) {
52 size_t ret = right - left;
70 template<
typename KeyType,
typename DataArrayType >
71 size_t custom_quicksort_inner( std::vector< KeyType > &key, DataArrayType &data,
72 const size_t left,
const size_t right,
const size_t pivot ) {
75 std::swap( key [ pivot ], key [ right-1 ] );
78 std::swap( data[ pivot ], data[ right-1 ] );
81 const BigInt pivotValue = key[ right-1 ];
84 size_t curIndex = left;
85 for(
size_t i = left; i < (right-1); ++i ) {
88 if( key[ i ] < pivotValue ) {
91 std::swap( key [ i ], key [ curIndex ] );
92 std::swap( data[ i ], data[ curIndex ] );
100 std::swap( key [ curIndex ], key [ right-1 ] );
101 std::swap( data[ curIndex ], data[ right-1 ] );
117 template<
typename KeyType,
typename DataArrayType >
118 void custom_quicksort_outer( std::vector< KeyType > &key, DataArrayType &data,
size_t left,
size_t right ) {
120 const size_t pivot = custom_quicksort_getpivot( key, left, right );
123 const size_t newPivotIndex = custom_quicksort_inner< KeyType, DataArrayType >( key, data, left, right, pivot );
126 if( newPivotIndex > left + 1 )
127 custom_quicksort_outer< KeyType, DataArrayType >( key, data, left, newPivotIndex );
128 if( newPivotIndex + 1 < right )
129 custom_quicksort_outer< KeyType, DataArrayType >( key, data, newPivotIndex + 1, right );
140 template<
typename KeyType,
typename DataType >
141 void custom_quicksort( std::vector< KeyType > &key, std::vector< DataType > &data,
size_t left,
size_t right ) {
142 const bool implicit =
sizeof( DataType ) > 2 *
sizeof(
size_t );
152 size_t *permutation =
new size_t[ right - left ];
155 for(
size_t i = left; i < right; ++i )
156 permutation[ i ] = i;
159 custom_quicksort_outer< KeyType, size_t* >( key, permutation, left, right );
162 std::vector< DataType > temp;
165 for(
size_t i = left; i < right; ++i )
166 temp.push_back( data[ permutation[ i ] ] );
169 for(
size_t i = left; i < right; ++i )
170 data[ i ] = temp[ i - left ];
173 delete [] permutation;
181 custom_quicksort_outer< KeyType, std::vector< DataType > >( key, data, left, right );
A 128-bit integer, with overloaded comparison operators.
Definition: BigInt.hpp:38