Binary Search 有时候我们也把它叫做二进制查找

是一种较为高效的再数组中查找目标元素的方法

我们可以通过递归和非递归两种方式来实现它

    //非递归
public static int binarySearch(int[] arr, int x) {
int low = 0;
int high = arr.length-1;
while(low <= high) {
int middle = (low + high)/2;
if(x == arr[middle]) {
return middle;
}else if(x <arr[middle]) {
high = middle - 1;
}else {
low = middle + 1;
}
}
return -1;
}
//递归实现二分查找
public static int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){
int midIndex = (beginIndex+endIndex)/2;
if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){
return -1;
}
if(data <dataset[midIndex]){
return binarySearch(dataset,data,beginIndex,midIndex-1);
}else if(data>dataset[midIndex]){
return binarySearch(dataset,data,midIndex+1,endIndex);
}else {
return midIndex;
}
}

  时间复杂度是O(log2 n)的,最差情况是log2(n+1)

二分查找(Binary Search)的递归和非递归的更多相关文章

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

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

  2. 二分查找(binary search)

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

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

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

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

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

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

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

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

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

  7. C语言实现 二分查找数组中的Key值(递归和非递归)

    基本问题:使用二分查找的方式,对数组内的值进行匹配,如果成功,返回其下标,否则返回 -1.请使用递归和非递归两种方法说明. 非递归代码如下: #include <stdio.h> int ...

  8. 【Weiss】【第03章】练习3.11:比较单链表递归与非递归查找元素

    [练习3.11] 编写查找一个单链表特定元素的程序.分别用递归和非递归实现,并比较它们的运行时间. 链表必须达到多大才能使得使用递归的程序崩溃? Answer: 实现都是比较容易的,但是实际上查找链表 ...

  9. C#实现(递归和非递归)高速排序和简单排序等一系列排序算法

        本人由于近期工作用到了一些排序算法.就把几个简单的排序算法.想冒泡排序,选择排序,插入排序.奇偶排序和高速排序等整理了出来,代码用C#代码实现,而且通过了測试.希望能给大家提供參考.     ...

随机推荐

  1. 傻瓜突破linux--rootpassword

    破password该方法: 方法1.单用户模式改动 (表示进入到单用户模式) ,按回车键,按b键启动.进入单用户模式.进行password改动,重新启动 init 5 口诀:e2e 空格1 回车b 开 ...

  2. OpenExpressApp:精通 WPF UI Virtualization

    原文:OpenExpressApp:精通 WPF UI Virtualization 本篇博客主要说明如何使用 UI Virtualization(以下简称为 UIV) 来提升 OEA 框架中 Tre ...

  3. Telnet协议详解(远程登陆协议)

    1. 概述 Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议.Telnet协议的目的是提供一个相对通用的,双向的,面向八位字节的通信方法,允许界面终端设备和面向终 ...

  4. 读BeautifulSoup官方文档之html树的打印

    prettify()能返回一个格式良好的html的Unicode字符串 : markup = '<a href="http://example.com/">I link ...

  5. Sql 执行查询顺序

  6. delphi文件操作(比较全)

    Delphi中默认有input和output两个文件变量,使用可以不用定义,直接使用. 但: input:只读.output:只写.用时注意以免引起异常. 文件是由文件名标识的一组数据的集合,文件通常 ...

  7. Win8Metro(C#)数字图像处理--2.4图像颜色聚类

    原文:Win8Metro(C#)数字图像处理--2.4图像颜色聚类  [函数名称] 图像颜色聚类函数ClusterProcess(WriteableBitmap src,int value) [算 ...

  8. Hamcrest、Mockito 专题

    UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...

  9. 零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」

    原文:零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」 本章将介绍UseLayoutRounding ...

  10. Android零基础入门第72节:SwipeRefreshLayout下拉刷新

    在实际开发中,经常都会遇到下拉刷新.上拉加载更多的情形,这一期就一起来学习Android系统的SwipeRefreshLayout下拉刷新组件. 一.SwipeRefreshLayout简介 Swip ...