算法实践:https://github.com/JackieLong/algorithms

常见查找算法:https://github.com/JackieLong/interview#%E6%9F%A5%E6%89%BE

二分查找

SearchAlgorithm.h

  1. namespace SearchAlgorithm
  2. {
  3. enum Algorithm
  4. {
  5. Binary,
  6. Binary_Recur,
  7. };
  8. // 拆半查找算法(循环版)
  9. template<class T, class Comp>
  10. int binarySearch( T t[], const int &data_size, const T &value, Comp comp )
  11. {
  12. int start = 0, end = data_size - 1, mid = -1;
  13. while( start <= end )
  14. {
  15. mid = ( start + end ) >> 1;
  16. if( t[mid] == value ) { return mid; }
  17. else if( comp( t[mid], value ) ) { start = mid + 1; }
  18. else { end = mid - 1; }
  19. }
  20. return -1;
  21. }
  22. // 拆半查找算法(递归版)
  23. template<class T, class Comp>
  24. int binarySearch_recur( T t[], const int &data_size, const T &value, Comp comp )
  25. {
  26. if( data_size <= 0 ) { return -1; }
  27. int mid = ( 0 + data_size - 1 ) >> 1;
  28. if( t[mid] == value ) { return mid; }
  29. else if( comp( t[mid], value ) ) { return _binarySearch_recur( t, data_size, value, mid + 1, data_size - 1, comp ); }
  30. else { return _binarySearch_recur( t, data_size, value, 0, mid - 1, comp ); }
  31. }
  32. template<class T, class Comp>
  33. int _binarySearch_recur( T t[], const int &data_size, const T &value, const int &start, const int &end, Comp comp )
  34. {
  35. if( start > end ) { return -1; }
  36. int mid = ( start + end ) >> 1;
  37. if( t[mid] == value ) { return mid; }
  38. else if( comp( t[mid], value ) ) { return _binarySearch_recur( t, data_size, value, mid + 1, end, comp ); }
  39. else { return _binarySearch_recur( t, data_size, value, start, mid - 1, comp ); }
  40. }
  41. }

测试用例

  1. #include "TestSearchAlgorithm.h"
  2. void main()
  3. {
  4. // ***************************************************************
  5. //
  6. // 测试查找算法
  7. //
  8. // ***************************************************************
  9. TestSearchAlgorithm testSearch;
  10. testSearch.test( SearchAlgorithm::Algorithm::Binary );
  11. testSearch.test( SearchAlgorithm::Algorithm::Binary_Recur );
  12. }

TestSearchAlgorithm.h

  1. #include "SearchAlgorithm.h"
  2. class TestSearchAlgorithm
  3. {
  4. using Algorithm = SearchAlgorithm::Algorithm;
  5. public:
  6. void test( const Algorithm algorithm );
  7. };

TestSearchAlgorithm.cpp

  1. #include "TestSearchAlgorithm.h"
  2. #include <functional>
  3. #include <algorithm>
  4. #include <map>
  5. #include <random>
  6. #include <iostream>
  7. void TestSearchAlgorithm::test( const Algorithm algorithm )
  8. {
  9. using T = int;
  10. using Comp = std::function<bool( const int &, const int & )>;
  11. using SearchFunc = std::function<int( T t[], const int &len, const T &value, Comp comp )>;
  12. using std::placeholders::_1;
  13. using std::placeholders::_2;
  14. using std::placeholders::_3;
  15. using std::placeholders::_4;
  16. std::map<Algorithm, SearchFunc> algorithmMap;
  17. algorithmMap[Algorithm::Binary] = std::bind( &SearchAlgorithm::binarySearch<T, Comp>, _1, _2, _3, _4 );
  18. algorithmMap[Algorithm::Binary_Recur] = std::bind( &SearchAlgorithm::binarySearch_recur<T, Comp>, _1, _2, _3, _4 );
  19. // *****************************************************
  20. // 随机生成一个升序的int数组,size=100。
  21. // *****************************************************
  22. static std::default_random_engine e;
  23. static std::uniform_int_distribution<int> u( 1, 100 );
  24. const int data_size = 100;
  25. T a[data_size];
  26. int last = 0;
  27. int tmpRandomInt = 0;
  28. for( int i = 0; i < data_size; i++ )
  29. {
  30. while( !tmpRandomInt ) { tmpRandomInt = u( e ); }
  31. a[i] = last + tmpRandomInt;
  32. last = a[i];
  33. tmpRandomInt = 0;
  34. }
  35. // *****************************************************
  36. // 输出随机数组
  37. // *****************************************************
  38. for( int i = 0; i < data_size; i++ ) printf( "[%d]%d ", i, a[i] );
  39. std::cout << std::endl << std::endl;
  40. // *****************************************************
  41. // 多次查找数组中随机的目标数,输出查找结果
  42. // *****************************************************
  43. static std::uniform_int_distribution<int> randomIndex( 0, data_size );
  44. int tmpTargetIdx;
  45. int tmpTarget = 0;
  46. for( int i = 0; i < data_size; i++ )
  47. {
  48. tmpTarget = a[randomIndex( e )];
  49. tmpTargetIdx = algorithmMap[algorithm]( a, data_size, tmpTarget, []( const int &a, const int &b ) {return a < b; } );
  50. printf( "result of searching target(%d) is a[%d] = %d\n", tmpTarget, tmpTargetIdx, a[tmpTargetIdx] );
  51. }
  52. }