折半搜索法(Binary Search)

    技术2022-05-11  1

    template<typename T>bool binary_search(T* A, T key, const int length){ int middle = length/2; while (key != A[middle] && middle > 0 && middle < length - 1) {  if(key > A[middle])  {   middle = (middle + length)/2;  }  else   middle = middle/2; } if(middle ==0 || middle == length - 1)  return key == A[middle]; return true;}


    最新回复(0)