算法实践:https://github.com/JackieLong/algorithms
常见查找算法:https://github.com/JackieLong/interview#%E6%9F%A5%E6%89%BE
二分查找
SearchAlgorithm.h
namespace SearchAlgorithm{enum Algorithm{Binary,Binary_Recur,};// 拆半查找算法(循环版)template<class T, class Comp>int binarySearch( T t[], const int &data_size, const T &value, Comp comp ){int start = 0, end = data_size - 1, mid = -1;while( start <= end ){mid = ( start + end ) >> 1;if( t[mid] == value ) { return mid; }else if( comp( t[mid], value ) ) { start = mid + 1; }else { end = mid - 1; }}return -1;}// 拆半查找算法(递归版)template<class T, class Comp>int binarySearch_recur( T t[], const int &data_size, const T &value, Comp comp ){if( data_size <= 0 ) { return -1; }int mid = ( 0 + data_size - 1 ) >> 1;if( t[mid] == value ) { return mid; }else if( comp( t[mid], value ) ) { return _binarySearch_recur( t, data_size, value, mid + 1, data_size - 1, comp ); }else { return _binarySearch_recur( t, data_size, value, 0, mid - 1, comp ); }}template<class T, class Comp>int _binarySearch_recur( T t[], const int &data_size, const T &value, const int &start, const int &end, Comp comp ){if( start > end ) { return -1; }int mid = ( start + end ) >> 1;if( t[mid] == value ) { return mid; }else if( comp( t[mid], value ) ) { return _binarySearch_recur( t, data_size, value, mid + 1, end, comp ); }else { return _binarySearch_recur( t, data_size, value, start, mid - 1, comp ); }}}
测试用例
#include "TestSearchAlgorithm.h"void main(){// ***************************************************************//// 测试查找算法//// ***************************************************************TestSearchAlgorithm testSearch;testSearch.test( SearchAlgorithm::Algorithm::Binary );testSearch.test( SearchAlgorithm::Algorithm::Binary_Recur );}
TestSearchAlgorithm.h
#include "SearchAlgorithm.h"class TestSearchAlgorithm{using Algorithm = SearchAlgorithm::Algorithm;public:void test( const Algorithm algorithm );};
TestSearchAlgorithm.cpp
#include "TestSearchAlgorithm.h"#include <functional>#include <algorithm>#include <map>#include <random>#include <iostream>void TestSearchAlgorithm::test( const Algorithm algorithm ){using T = int;using Comp = std::function<bool( const int &, const int & )>;using SearchFunc = std::function<int( T t[], const int &len, const T &value, Comp comp )>;using std::placeholders::_1;using std::placeholders::_2;using std::placeholders::_3;using std::placeholders::_4;std::map<Algorithm, SearchFunc> algorithmMap;algorithmMap[Algorithm::Binary] = std::bind( &SearchAlgorithm::binarySearch<T, Comp>, _1, _2, _3, _4 );algorithmMap[Algorithm::Binary_Recur] = std::bind( &SearchAlgorithm::binarySearch_recur<T, Comp>, _1, _2, _3, _4 );// *****************************************************// 随机生成一个升序的int数组,size=100。// *****************************************************static std::default_random_engine e;static std::uniform_int_distribution<int> u( 1, 100 );const int data_size = 100;T a[data_size];int last = 0;int tmpRandomInt = 0;for( int i = 0; i < data_size; i++ ){while( !tmpRandomInt ) { tmpRandomInt = u( e ); }a[i] = last + tmpRandomInt;last = a[i];tmpRandomInt = 0;}// *****************************************************// 输出随机数组// *****************************************************for( int i = 0; i < data_size; i++ ) printf( "[%d]%d ", i, a[i] );std::cout << std::endl << std::endl;// *****************************************************// 多次查找数组中随机的目标数,输出查找结果// *****************************************************static std::uniform_int_distribution<int> randomIndex( 0, data_size );int tmpTargetIdx;int tmpTarget = 0;for( int i = 0; i < data_size; i++ ){tmpTarget = a[randomIndex( e )];tmpTargetIdx = algorithmMap[algorithm]( a, data_size, tmpTarget, []( const int &a, const int &b ) {return a < b; } );printf( "result of searching target(%d) is a[%d] = %d\n", tmpTarget, tmpTargetIdx, a[tmpTargetIdx] );}}
