二分法查找适用于排列有序的数据。java实现方法如下:

	// Find the location of a value in array a
// Array a must be sorted
// Return -1, if the value is not in a
public static int binarySearch(int value, int[] a) { int lo = 0;
int hi = a.length -1;
int mid = lo + (hi - lo) / 2; while(lo <= hi) { if (value > a[mid]) lo = mid + 1;
else if (value < a[mid]) hi = mid - 1;
else return mid; mid = lo + (hi - lo) / 2;
} return -1;
}

有几点需要说明:

while(lo <= hi)  必须是 <=  。若缺少=,则数组的最后一个数据一定查找失败。

每次循环lo = mid + 1;hi = mid - 1;必须加上或减去-,否则可能造成死循环。

如: 当lo与hi相邻时,lo的位置为i,hi的位置为i+1,则mid = lo或hi,无法改变hi与lo的位置,陷入死循环。

二分法查找 (Binary Search)的更多相关文章

  1. 二分法查找(Binary Search)

    --摘要:二分法的介绍已经很多了,但并不直观,因此此文诞生,希望批评指正. 二分查找是在有序数组中查找一个元素的算法,通过比较目标元素与数组中间元素来查找,如果目标值是中间元素则将返回中间元素位置. ...

  2. STL之二分查找 (Binary search in STL)

    STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...

  3. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  4. LeetCode编程训练 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  5. 算法与数据结构基础 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  6. LeetCode 704. 二分查找(Binary Search)

    704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...

  7. 二分查找(binary search)

    二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...

  8. 数据结构-二分查找(Binary Search)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

  9. [Swift]LeetCode704. 二分查找 | Binary Search

    Given a sorted (in ascending order) integer array nums of nelements and a target value, write a func ...

  10. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

随机推荐

  1. hibernate一级缓存和二级缓存的区别

    http://blog.csdn.net/defonds/article/details/2308972 缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了 ...

  2. forget word _a

    forget word a~   一再,铺音前     1● ab 2● ac 3● ad 4● af 5● ag 6● an 7● as 8● at 9● ap 10● ar

  3. POJ 1847 Floyd_wshall算法

    前面用dijstra写过了.但是捏.数据很小.也可以用Floyd来写. 注意题目里给出的是有向的权值. 附代码:#include<stdio.h>#include<string.h& ...

  4. sgu107. 987654321 problem 简单打表 难度:0

    107. 987654321 problem time limit per test: 0.25 sec. memory limit per test: 4096 KB For given numbe ...

  5. Python中简化的验证码功能实现

    #!/usr/bin/env python # _*_ encoding:utf-8 _*_ # author:snate import random def generate_auth_code() ...

  6. Our supersheet

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. weblogic下更改jsp不生效的解决办法

    1.删除user_projects\domains\base_domain(我自己的域名)\servers\AdminServer目录下的所有文件夹 一下为转载from:http://blog.itp ...

  8. [需要补充]javaEE中servlet方法service与doXXX的关系

    刚开始很模糊他们的关系,不清楚 service protected void service(HttpServletRequest req, HttpServletResponse resp) thr ...

  9. sring 监听器

    参考链接:http://blog.csdn.net/ysughw/article/details/8992322 sring容器在web程序中获取的方式:http://blog.csdn.net/aq ...

  10. ipython与sublime调用其shell出现的问题

    本机电脑 win10 已安装python3.5 1.    直接在命令行运行 pip install ipython[all] 安装 ipython 安装完成后 在命令行输入 jupyter note ...