小编Joh*_*tin的帖子

二元搜索的逻辑

我在数据结构书中读到了二进制搜索的伪代码,然后我开始编写代码.我写的代码是:

#include <iostream.h>
#include <conio.h>
template <class T>

int BSearch(T x[], const int n, T item)
    {
    int loc, first = 0, found = 0, last = n-1;
        while(first <= last && !found)
        {
            loc = (first + last)/2;
            if(item < x[loc])
                last = loc - 1;
            else if(item > x[loc])
                first = loc + 1;
            else
                found = 1;
         }
      return found;
   }

int main()
    {
    const int n =5;
      int x[n],item;
      cout << "Pls enter " <<n<<" number(s): …
Run Code Online (Sandbox Code Playgroud)

c c++ binary-search

3
推荐指数
1
解决办法
1303
查看次数

标签 统计

binary-search ×1

c ×1

c++ ×1