文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>二分查找

二分查找

时间:2011-04-24  来源:qingcheng奕

#include<iostream>
using namespace std;

int BinarySearch(int a[],int n,int goal)
{
 for(int i=0;i<=n;i++)
 cout<<a[i]<<endl;
 int mid,low=0,high=n;
 while(low<=high)
 {
  mid=(low+high)/2;
  if(goal==a[mid])
   return mid;
  else if(goal<a[mid])
   high=mid-1;
  else
   low=mid+1;
 }
 return -1;
}
int main()
{
 int arr[10]={0,3,7,9,11,35,66,78,89,100};
 int a;
 while(cin>>a)
 {
  int b;
  b=BinarySearch(arr,9,a);
  cout<<b<<endl;
 }
 return 0; 
}

//非递归算法

int BinarySearch()

{

      int low=0,high=n-1;

      int temp;

      while(low<=high)

      {

           temp=(low+high)/2;

           if(k==R[temp])

           {

                 return temp;

           }

           else if(k<R[temp])

           {

                 high=temp-1;

           }

           else

           {

                 low=temp+1;

           }

      }

      return -1;

}

相关阅读 更多 +
排行榜 更多 +
方块枪战战场安卓版

方块枪战战场安卓版

飞行射击 下载
战斗火力射击安卓版

战斗火力射击安卓版

飞行射击 下载
空中防御战安卓版

空中防御战安卓版

飞行射击 下载