【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 ...
随机推荐
- stm32进入HardFault_Handler的定位方法
写程序偶尔会遇到程序死机的现象.这个时候,就需要debug来定位. 通常情况下,程序会进入HardFault_Handler的死循环(针对stm32系列),我遇到过两次. 第一次是使用数组之前,数组的 ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- Centos7 minimal 安装npm
最小版本缺少很多源,需要手动去添加源 如何去判断yum中 有没有 npm 的源呢 yum list | grep npm 如果是这样的,就代表需要自己去添加 curl -sL -o /etc/yum. ...
- mysql 8.0.17 安装配置方法图文教程
1.URL:https://www.jb51.net/article/167782.htm 2.装好之后需要使用add user中的用户名和密码登录(之前安装数据库时出现的) 使用navicat连接时 ...
- asp.net实现大文件上传分片上传断点续传
HTML部分 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.a ...
- Zabbix 短信报警示例
Zabbix 短信报警 示例: 注意zabbix 脚本文件默认放置目录是 alertscripts (zabbix 动作调用脚本目录) # 编辑 zabbix_server.conf # AlertS ...
- Webstorm 2019最新激活码
Webstorm 2019激活码(有效期至2020年6月5日) K6IXATEF43-eyJsaWNlbnNlSWQiOiJLNklYQVRFRjQzIiwibGljZW5zZWVOYW1lIjo ...
- 复习题之Blah数集
题目描述: 大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于已a为基的集合Ba定义如下: (1)a是集合Ba的基,且a是Ba的第一个元素: (2)如果x在集合Ba中,则2x+1,3x+1 ...
- c++学习知识整理
<iomanip>传送门:https://baike.baidu.com/item/iomanip/3319954?fr=aladdin linux为何用./运行程序:https://bl ...
- AtCoder Grand Contest 017题解
传送门 \(A\) 直接转移就是了 typedef long long ll; const int N=55; ll f[N][2];int a[N],n,p; int main(){ scanf(& ...