二分搜索

2016-12-21  本文已影响0人  MingweiLee
Template <Class Type>
int binarySearch(Type a[] , const T&x , int left , int right , int &i , int &j)
{
      int middle;
      while(left <= right)
        {
              middle = (left + right) / 2;
              if(x == a[middle])
              {
                  i = j =middle;
                  return 1;
              }
              if(x < a[middle]) right = middle - 1;
              else   left = middle + 1;
        }
      return 0 ;
}
上一篇下一篇

猜你喜欢

热点阅读