博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对半搜索
阅读量:6985 次
发布时间:2019-06-27

本文共 612 字,大约阅读时间需要 2 分钟。

#include 
using namespace std;int a[5]={1,2,3,4,5};int Bsearch(int x, int left,int right ){ if(left<=right) { int m=(left+right)/2; if(x
a[m]) return Bsearch(x,m+1,right); else return m; } else return -1;}int main(){ int m=Bsearch(3,0,4); if(m==-1)cout<<"搜索失败"<

  下面再来一个迭代的:

#include 
using namespace std;int main(){ int a[5]={1,2,3,4,5};//已排好序了 int left =0; int right =4; int x=6; while(left<=right) { int m=(left+right)/2; if(x
a[m]) left =m+1; else {cout<<"搜索成功"<

  

转载于:https://www.cnblogs.com/devil-91/archive/2012/06/20/2556614.html

你可能感兴趣的文章
C语言宏的副作用的简单实例
查看>>
关于C语言结构体对齐的学习
查看>>
富文本框
查看>>
windows下安装rabbitMQ
查看>>
20个优秀的移动(iPhone)网站设计案例
查看>>
CentOS 6.3安装Nginx开启目录浏览、下载功能
查看>>
oracle登陆认证方式
查看>>
FMDB/SQLCipher数据库管理
查看>>
cocos_python
查看>>
关于安装oracle 11G R2 for Windows X64问题
查看>>
springmvc 重定向传递参数
查看>>
tomcat实现session集群及tomcat+memcached共享session存储(四)
查看>>
线性时间排序--桶排
查看>>
Three.js学习笔记
查看>>
ceph-deploy部署bluestore
查看>>
AIX修改系统时间 命令
查看>>
Window_Open详解
查看>>
Codeforces 606-C:Sorting Railway Cars(LIS)
查看>>
eclipse ldt update resource
查看>>
java-HTML&javaSkcript&CSS&jQuery&ajax
查看>>