当前位置:网站首页>Binary search / half search
Binary search / half search
2022-07-22 11:30:00 【p29949】
// Find a specific number in an ordered array n.
//1 2 3 4 5 6 7 8 9 10
// Two points search / Binary search
#include<stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);// The number of elements in the array
int left = 0;
int right = sz-1;
int k = 0;
scanf("%d", &k);
while (left<=right)
{
//int mid = (left + right) / 2;
int mid = left + (right - left) / 2;
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else
{
printf(" eureka , The subscript is :%d\n", mid);
break;
}
}
if (left > right)
printf(" Can't find \n");
return 0;
}
边栏推荐
猜你喜欢
Opencv && 把视频裁剪成指定帧率的图像集
nodes 简介
nodes 簡介
力扣------统计有序矩阵中的负数
Recommended reading: how can testers get familiar with new businesses quickly?
[解决]vscode代码没有输入完成就自动换行bug
systemd 管理 blackbox-exporter
Intel E3 V1 V2 V3 CPU parameters
即看即用 && Reduction Ops && Pytorch官方文档总结 && 笔记 (五)
即看即用 && 其他操作(Other Operations) && Pytorch官方文档总结 && 笔记 (八)
随机推荐
标签平滑(LabelSmoothing)介绍与代码实现
《Multiple UAV exploration of an unknown region》翻译
ICML 2022开奖!复旦、上交、厦大多篇工作入选杰出论文
Which securities are good for opening an account for stock speculation? Is it safe to open an account online
Illustrate the beginning of NLP transfer learning by Bert, Elmo, etc
关于编写安全的智能合约
Custom type: structure (II) bit segment implementation
嵌入式分享合集18
Is the flush platform safe? Huatai Securities Account Opening app
X Book app digital beauty Sid analysis
即看即用 && 其他操作(Other Operations) && Pytorch官方文档总结 && 笔记 (八)
nodes 简介
Routing policy-
CKeditor 中的粘贴不起作用(您的浏览器不支持通过工具栏或右键菜单进行粘贴,请按 Ctrl+V 进行粘贴)
PHP生成器的使用yield性能优化
【学习笔记】带你从0开始学习 01Trie
什么是因果深度学习?DeepMind最新ICML2022《因果性与深度学习:协同、挑战和未来》教程
Sentinel vs Hystrix 对比,你怎么选?
基于PyTorch深度学习遥感影像地物分类与目标检测、分割及遥感影像问题深度学习优化
OSPF comprehensive experiment