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

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

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

  1. //非递归
  2. public static int binarySearch(int[] arr, int x) {
  3. int low = 0;
  4. int high = arr.length-1;
  5. while(low <= high) {
  6. int middle = (low + high)/2;
  7. if(x == arr[middle]) {
  8. return middle;
  9. }else if(x <arr[middle]) {
  10. high = middle - 1;
  11. }else {
  12. low = middle + 1;
  13. }
  14. }
  15. return -1;
  16. }
  17. //递归实现二分查找
  18. public static int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){
  19. int midIndex = (beginIndex+endIndex)/2;
  20. if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){
  21. return -1;
  22. }
  23. if(data <dataset[midIndex]){
  24. return binarySearch(dataset,data,beginIndex,midIndex-1);
  25. }else if(data>dataset[midIndex]){
  26. return binarySearch(dataset,data,midIndex+1,endIndex);
  27. }else {
  28. return midIndex;
  29. }
  30. }

  时间复杂度是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. 设置oracle密码不过期,修改用户密码

    1. 查看用户名使用的profile select username,profile from dba_usersSELECT * FROM dba_profiles WHERE profile='D ...

  2. 各linux版本重启apache命令

    各linux版本重启apache命令 Slackware Linux命令: /etc/rc.d/rc.httpd restart ubuntu.Debian 系统命令: /etc/init.d/apa ...

  3. 【C#】【WPF】如何读写app.config文件

    WPF生成的项目中会有.exe.config.一般是系统默认配置的 格式是xml格式,C#的项目可以直接读写这些文件.方法代码如下. public static string GetConnectio ...

  4. 早期malloc分配时,如果内存耗尽分配不出来,会直接返回NULL。现在分配不出来,直接抛出异常(可使用nothrow关键字)

    今天和同事review代码时,发现这样的一段代码: Manager * pManager = new Manager(); if(NULL == pManager) { //记录日志 return f ...

  5. XML Serialize/Deserialize

    using System; using System.Collections.Generic; using System.Globalization; using System.IO; using S ...

  6. 备份一个支持虚拟化的wrappanel

    public class VirtualizingWrapPanel : VirtualizingPanel, IScrollInfo { #region Fields UIElementCollec ...

  7. C#代码中设置 控件的触发器

    Style style = new Style(); style.TargetType = typeof(TextBox); MultiDataTrigger trigger = new MultiD ...

  8. Win8Metro(C#)数字图像处理--2.30直方图均衡化

    原文:Win8Metro(C#)数字图像处理--2.30直方图均衡化 [函数名称] 直方图均衡化函数HistogramEqualProcess(WriteableBitmap src) [算法说明] ...

  9. dotnetspider

    http://www.cnblogs.com/modestmt/p/5525467.html nuget :DotnetSpider2.Core

  10. 【Qt】无边框窗体中带有ActiveX组件时的一个BUG

    无意中发现的一个BUG,Qt5.1.1正式版首先创建一个GUI工程,拖入一个QAxWidget控件(为了使ActiveX生效,需要在.pro文件中加入CONFIG += qaxcontainer)接着 ...