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. java oracle的2种分页方法

    java oracle的2种分页方法 一物理分页: <!-- 分页查询所有的博客信息 --> <select id="findBlogs" resultType= ...

  2. 常用的两种web单点登录SSO的实现原理

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...

  3. 洛谷 P816 忠诚 题解

    每日一题 day28 打卡 Analysis 这道题用线段树维护区间最小值很简单,因为没有修改所以连lazy_tag都不用,但是这道题可以用树状数组维护区间最小值,非常骚气. 线段树代码: #incl ...

  4. 017_Python3 数据结构

    本章节我们主要结合前面所学的知识点来介绍Python数据结构.   *****************************   1.列表 Python中列表是可变的,这是它区别于字符串和元组的最重 ...

  5. 【EF】vs2017中没有EF模型

    在添加->新建项目 中找不到实体模型? 或者 在vs中打开edmx文件时,显示的只有文本,没有图形模式 原因:是因为没有安装实体模型插件 解决方法: 1.打开网址 https://marketp ...

  6. 洛谷 P1191 矩形 题解

    P1191 矩形 题目描述 给出一个 \(n \times n\)的矩阵,矩阵中,有些格子被染成白色,有些格子被染成黑色,现要求矩阵中白色矩形的数量 输入格式 第一行,一个整数\(n\),表示矩形的大 ...

  7. pcl-qt使用QVTKWidget 与PCLVisualizer 显示雷达点云

    #ifndef PCLVIEWER_H #define PCLVIEWER_H #include "defines.h" #include <iostream> #in ...

  8. AtCoder Grand Contest 012题解

    传送门 \(A\) 肯定是后面每两个陪最前面一个最优 typedef long long ll; const int N=5e5+5; int a[N],n;ll res; int main(){ s ...

  9. 【一起来烧脑】读懂WebApp知识体系

    背景 很多小白知道什么是app,但是却不知道什么是webapp呢,webapp是指用HTML5编写的移动web应用 一个webapp可以在pc端,Android端,ios端进行运行 webapp开发的 ...

  10. shell编程题(三)

    将一目录下所有的文件的扩展名改为bak #! /bin/bash for i in `ls` do mv $i ${i%%.*}.bak done ${i%%.*} 截掉一个变量字符串第一个" ...