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. 查看mysql日志文件

    开启mysql日志 /etc/mysql/mysql.conf.d/mysqld.cnf sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 找到第68,69行 前面 ...

  2. [GCP] Goolge compute Engine

    Which of the following is a PAAS option for hosting web apps on GCP? App Engine standard or flexible ...

  3. PostgreSQL 恢复大法 - 恢复部分数据库、跳过坏块、修复无法启动的数据库

    转载自:https://yq.aliyun.com/articles/582880 背景 一个较大的数据库,如何只恢复一部分数据(例如只恢复某个DB). 如果访问有坏块的表. 如何从无法启动的数据库中 ...

  4. YAML_04 用user模块添加用户,并修改密码

    ansible]# vim user.yml --- - hosts: cache   remote_user: root   vars:     username: lisi   tasks:   ...

  5. web实现大文件上传分片上传断点续传

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...

  6. ServletContext对象应用——三天免登录

    1.用到的知识点: (1)Cookie (2)Session (3)ServletContext 其中Cookie和Session是会话技术的组成部分,一次会话从打开浏览器的某个站点开始,到浏览器关闭 ...

  7. linux 安装gcc 和 g++

    以CentOS为例,安装后是没有C语言和C++编译环境的,需要手动安装,最简单的是用yum的方式安装,过程如下: 1.安装gcc yum install gcc 询问是否,按y键回车即可,或者 yum ...

  8. 本地Windows远程桌面连接阿里云Ubuntu服务器

    本地Windows远程桌面连接阿里云Ubuntu 16.04服务器: 1.目的:希望通过本地的Windows远程桌面连接到阿里云的Ubuntu服务器,通过远程桌面图形界面的方式操作服务器. 2.条件: ...

  9. 服务器上build.xml文件乱码解决(亲测有效)

    前提条件:必须root账户登录系统,否则无权限 1. 修改/etc/sysconfig/i18n: 拷贝如下内容到文件中 #LANG="zh_CN.UTF-8" LANG=&quo ...

  10. vim配图

    https://blog.csdn.net/zhlh_xt/article/details/52458672 https://www.jianshu.com/p/75cde8a80fd7 https: ...