C++编程之美-结构之法(代码清单3-17(带有错误的二分查找源码))

代码清单3-17(带有错误的二分查找源码)

int bisearch(char** arr, int b, int e, char* v)
{
     int minIndex = b, maxIndex = e, midIndex;
     while(minIndex < maxIndex)
     {
          midIndex = (minIndex + maxIndex) / 2;
          if(strcmp(arr[midIndex], v) <= 0)
          {
               minIndex = midIndex;
          }
          else
          {
               maxIndex = midIndex – 1;
          }
     }
     if(!strcmp(arr[maxIndex], v))
     {
          return maxIndex;
     }
     else
     {
          return -1;
     }
}
发布了1246 篇原创文章 · 获赞 951 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/104028171