惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
博客园 - Franky
罗磊的独立博客
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
V
V2EX
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
U
Unit 42
GbyAI
GbyAI
A
About on SuperTechFans
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
D
DataBreaches.Net
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog

博客园 - 浅蓝

Anaconda docker Ubuntu18.04安装Tensorflow1.14GPU matplotlib中color可用的颜色 TensorFlow升级到1.13 配置VPN - 浅蓝 Ubuntu16.04 安装Tensorflow1.7过程记录二:安装CUDA及Tensorflow tensorflow 源码编译 Ubuntu16.04 安装Tensorflow1.7过程记录一:安装显卡驱动 深度学习实验记录 ubuntu16.04安装tensorflow1.3 深度学习开源代码链接 如何高效的学习 TensorFlow 代码? 谷歌发布了 T2T(Tensor2Tensor)深度学习开源系统 学习Tensorflow的LSTM的RNN例子 tensorflow nan TensorFlow数据读取 TensorFlow笔记之常见七个参数 tf-slim-mnist
c++
浅蓝 · 2021-10-25 · via 博客园 - 浅蓝

C++ STL中的Binary search(二分查找) - 王陸 - 博客园 (cnblogs.com)

#include <cstdio>
#include <algorithm>
using namespace std;
int a[100]= {4,10,11,30,69,70,96,100};
int binarySearch(int x,int n)
{
int left =0;
int right=n-1;
while(left<=right)
{
int mid =(left+right)/2;
if(x==a[mid])
{
return mid;
}
if(x>a[mid])
{
left=mid+1;
}
else
{
right =mid-1;
}
}
return -1;//未找到x
}
//二分搜索递归实现
int recurisonBinarySearch(int left,int right,int x)
{
if(left>right)
{
return -1;
}
int mid =(left+right)/2;
if(x==a[mid])
{
return mid;
}
if(x>a[mid])
{
return recurisonBinarySearch(mid+1,right,x);
}
else
{
return recurisonBinarySearch(left,mid-1,x);
}
}
int main()
{
int x;
int ans1,ans2;
scanf("%d",&x);
ans1=binarySearch(x,8);
ans2=recurisonBinarySearch(0,7,x);
printf("%d %d\n",ans1,ans2);
return 0;
}