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,二分搜索算法的更多相关文章

  1. js binary search algorithm

    js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...

  2. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  3. [Math] Beating the binary search algorithm – interpolation search, galloping search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  4. [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  5. C++ STL中的Binary search(二分查找)

    这篇博客转自爱国师哥,这里给出连接https://www.cnblogs.com/aiguona/p/7281856.html 一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返 ...

  6. LeetCode Binary Search Summary 二分搜索法小结

    二分查找法作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围,大大缩短了搜索时间,具有很大的应用场景,而在LeetCode中,要运用二分搜索法来解的题目也有很多,但是实际上二分查找法的查找目 ...

  7. Binary Search Algorithm

    二分查找代码: //============================================================================ // Name : Bin ...

  8. [转]C++ STL中的Binary search(二分查找)

    链接地址:https://www.cnblogs.com/wkfvawl/p/9475939.html

  9. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

随机推荐

  1. Intellij IDEA – How to build project automatically

    By default, Intellij IDEA doesn’t compile classes automatically. But, you can enable the auto compil ...

  2. memset()函数的使用

    1.概述 memset()函数,称为按字节赋值函数,使用时需要加头文件 #include<cstring>或者#include<string.h>.通常有两个用法: (1)用来 ...

  3. 物体检测方法(1) - YOLO 详解

    最近遇到一些卡证识别的检测问题,打算先把理论知识梳理一下,随后还会梳理一版代码注释. 以前的region+proposal来检测的框架,这一系列速度和精度不断提高,但是还是无法达到实时.存在的主要问题 ...

  4. b、B、KB、MB、GB 的关系?

    1. 8bit (位) = 1Byte (字节) 2.1024Byte (字节 ) = 1KB 3.1024KB = 1MB 4.1024MB = 1GB 5.1024GB = 1TB

  5. LibreOJ #108. 多项式乘法

    二次联通门 : LibreOJ #108. 多项式乘法 /* LibreOJ #108. 多项式乘法 FFT板子题 不行啊...跑的还是慢 应该找个机会学一学由乃dalao的fft 或者是毛爷爷的ff ...

  6. shell命令的原理

    https://blog.csdn.net/m0_37925202/article/details/80258974 https://blog.csdn.net/a15929748502/articl ...

  7. 建立自己的键盘栈(shortcutkeyStack)

    建立自己的键盘栈(shortcutkeyStack) 作为一名开发者, 快捷键是必不可少的, 并且各种开发工具都有提供快捷键. 但是各种工具(IDE,编辑器)因为历史或者其他不可抗原因(比如键盘的布局 ...

  8. fgets()函数

    声明: char *fgets(char *str,int n,FILE* stream) 参数: str—这是指向一个字符数组的指针,该数组存储了要读取的字符串 n – 这是要读取的最大字符数(包括 ...

  9. ZwQuerySystemInfoMation函数使用

    ZwQueryInfoMation函数很简单.就是4个参数. NTSTATUS WINAPI ZwQuerySystemInformation( _In_ SYSTEM_INFORMATION_CLA ...

  10. C++标准库分析总结(九)——<HashFunction、Tuple>

    一.HashFunction 当我们在使用hash table以及由它做底层的数据结构时,我们必不可少要讨论hash function,所谓的哈希函数就是产生一个数,这个数越乱越好,以至于达到避免碰撞 ...