Iterative ways:

 int binarySearch (int[] a, int x) {
int low = 0;
int high = a.length - 1;
int mid; while (low <= high) {
mid = (low + high) / 2;
if (a[mid] < x) {
low = mid + 1;
}
else if (a[mid] > x) {
high = mid - 1;
}
else {
return mid;
}
}
return -1;
}

Recursive ways:

 int binarySearchRecursive (int[] a, int x, int low, int high) {
if (low > high) return -1; //error int mid = (low + high) / 2;
if (a[mid] < x) {
return binarySearchRecursive(a, x, mid + 1, high);
}
else if (a[mid] > x) {
return binarySearchRecursive(a, x, low, mid - 1);
}
else {
return mid;
}
}

Summary: Binary Search的更多相关文章

  1. Binary Search Tree Learning Summary

    BST Definition BST is short for Binary Search Tree, by definition, the value of right node is always ...

  2. Unique Binary Search Trees [LeetCode]

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  4. Method for balancing binary search trees

    Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...

  5. 算法 binary search

    // ------------------------------------------------------------------------------------------------- ...

  6. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. Artech的MVC4框架学习——第三章controller的激活

    第一当目标controller的名称通过URL路由被解析出来后,asp.net mvc利用 ControllerBuilder 注册 ControllerFactory ,根据名称实现对目标contr ...

  2. Centos 升级 python

    昨天把redmine的测试环境给搞Over了,想了下,干脆直接把环境给整成docker化的,配置环境的时候,安装docker-compose需要python2.7支持. CentOS 6 系统默认 P ...

  3. MFC修改窗口无标题和标题信息,修改执执行文件图标

    一.创建MFC后 窗口显示的是 无标题-工程名 修改方法在网上看到了几种,下面介绍下比较简单的一种: 1.在MianFrame.c文件中找到这个函数 BOOL CMainFrame::PreCreat ...

  4. 9.10Django模板

    2018-9-10 16:37:29 模板就一个 不能嵌套 模板:  http://www.cnblogs.com/liwenzhou/p/7931828.html 2018-9-10 21:23:3 ...

  5. php base64转图片

    1.解析base64数据成图片 The problem is that data:image/bmp;base64, is included in the encoded contents. This ...

  6. C#调用C++ DLL的方式

    动态链接库(DLL)是一个包含可由多个程序同时使用的代码和数据的库,DLL不是可执行文件.可以说在windows操作系统中随处可见,打开主分区盘下的system32.在一些项目中,如果有大量运算或者涉 ...

  7. Linux监控远程端口是否开启脚本

    #!/bin/bash #author Liuyueming #date 2017-07-29 #定时检测邦联收单及预付卡系统 pos_num=`nmap 远程IP地址 -p 端口号|sed -n & ...

  8. Solr学习笔记之问题汇总

    一. 问题描述:Solr在建立索引时候出现如下错误:org.apache.solr.common.SolrException: Document [null] missing required fie ...

  9. POJ - 1191 棋盘分割 记忆递归 搜索dp+数学

    http://poj.org/problem?id=1191 题意:中文题. 题解: 1.关于切割的模拟,用递归 有这样的递归方程(dp方程):f(n,棋盘)=f(n-1,待割的棋盘)+f(1,割下的 ...

  10. MTD 移动目标防御技术

    移动目标防御技术,主要包括系统随机化,生物启发MTD,网络随机化,云MTD,动态编译等等.研讨会还就威胁建模和量化移动目标防御技术的效能评估进行了推进.理论和定量的模型对于该技术的颠覆性影响至关重要. ...