#ifndef CA_UWATERLOO_ALUMNI_DWHARDER_SEARCHING_ALGORITHMS_INTERPOLATION_SEARCH_ARRAY #define CA_UWATERLOO_ALUMNI_DWHARDER_SEARCHING_ALGORITHMS_INTERPOLATION_SEARCH_ARRAY #include "Binary_search_array.h" namespace Searching_algorithms { namespace Interpolation_search { int const ARRAY_USE_BINARY = 500; class Array { public: template static bool search( Type const &obj, Type *array, int n ) { if ( obj < array[0] || array[n - 1] < obj ) { return false; } return search_iterative( obj, static_cast( obj ), array, 0, n - 1 ); } template static bool search( Type const &obj, Type *array, int a, int b ) { if ( obj < array[a] || array[b] < obj ) { return false; } return search_iterative( obj, static_cast( obj ), array, a, b ); } private: template static bool search_iterative( Type const &obj, double d_obj, Type *array, int a, int c ) { while ( c - a >= ARRAY_USE_BINARY ) { if ( array[a] == array[c] ) { return array[a] == obj; } int b = a + static_cast( static_cast( c - a )*(d_obj - array[a])/static_cast(array[c] - array[a]) ); if ( array[b] == obj ) { return true; } else if ( obj < array[b] ) { c = b - 1; if ( array[c] < obj ) { return false; } } else { a = b + 1; if ( array[a] > obj ) { return false; } } } return Searching_algorithms::Binary_search::Array::search( obj, array, a, c ); } }; } } #endif