【437】Binary search algorithm,二分搜索算法
Complexity: O(log(n))
Ref: Binary search algorithm or 二分搜索算法
Ref: C 版本 while 循环
C Language scripts by McDelfino:
#include <stdio.h>
#include <stdlib.h> int getIndex(int* arr, int length, int num); int main() {
int arr[100]; for (int i = 0; i < 100; i++) {
arr[i] = i + 1;
} for (int i = 1; i <= 100; i++) {
printf("num = %d, find index = %d.\n", i, getIndex(arr, 100, i));
} return 0;
} int getIndex(int* arr, int length, int num) {
int min = 0;
int max = length - 1;
int count = 0;
while (1) {
int avg = (min + max)/2;
count++;
if (arr[avg] < num) min = avg + 1;
else if (arr[avg] > num) max = avg - 1;
else {
printf("-----count = %d-----\t", count);
return avg;
}
}
}

【437】Binary search algorithm,二分搜索算法的更多相关文章
- js binary search algorithm
js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- [Math] Beating the binary search algorithm – interpolation search, galloping search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- C++ STL中的Binary search(二分查找)
这篇博客转自爱国师哥,这里给出连接https://www.cnblogs.com/aiguona/p/7281856.html 一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返 ...
- LeetCode Binary Search Summary 二分搜索法小结
二分查找法作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围,大大缩短了搜索时间,具有很大的应用场景,而在LeetCode中,要运用二分搜索法来解的题目也有很多,但是实际上二分查找法的查找目 ...
- Binary Search Algorithm
二分查找代码: //============================================================================ // Name : Bin ...
- [转]C++ STL中的Binary search(二分查找)
链接地址:https://www.cnblogs.com/wkfvawl/p/9475939.html
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
随机推荐
- 【Mac电脑】Burp 设置代理后提示“您的链接不是私密链接”的解决方案
一.遇到的问题 通过Burp Proxy代理抓包时,遇到的如下情况: 二.解决问题 (1)基础介绍 HTTPS协议是为了数据传输安全的需要,在HTTP原有的基础上,加入了安全套接字层SSL协议,通过C ...
- Arrays.asList 存在的坑
引语: 阿里巴巴java开发规范说到使用工具类Arrays.asList()方法把数组转换成集合时,不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportedO ...
- JAVA读取XML并打印
在G盘下新建XML文档:person.xml,XML代码: <?xml version="1.0" encoding="utf-8"?> <s ...
- Linux 一条长命令占用多行
前言 考察下面的脚本: ? 1 emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --so ...
- LIO -SCSI target
2010年底,LIO 项目获选成为新的内核态的 SCSI target,取代原有的用户态的 STGT 项目.当时有两个主要的竞争项目(LIO和SCST),都在努力将代码并入主线内核.本文将比较着两个项 ...
- 利用fgetc合并2个源文件的内容,到一个新的文件中
#include <stdio.h> #include <stdlib.h> //功能: 合并2个源文件的内容,到一个新的文件中 int main(int a,char *ar ...
- 搭建K8S集群
一.前言 我们将现有的虚拟机称之为Node1,用作主节点.为了减少工作量,在Node1安装Kubernetes后,我们利用VirtualBox的虚拟机复制功能,复制出两个完全一样的虚拟机作为工作节点. ...
- html5中的fieldset/legend元素和keygen元素
html5中的fieldset/legend元素和keygen元素 一.总结 一句话总结: fieldset/legend元素和figure和figcaption很像,只不过是作用于表单,前者表示内容 ...
- Makefile教程(绝对经典,所有问题看这一篇足够了)【转】
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客: http://blog.csdn.net/haoel/article/details/2886 makefile很重 ...
- 阿里druid连接池监控数据自定义存储
如何将druid连接池监控到的sql执行效率,连接池资源情况等进行持久化存储,方便系统运维分析优化,以下案例初步测试成功. 第一部: 新建MyDruidStatLogger类实现接口 extends ...