Summary: Binary Search
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的更多相关文章
- Binary Search Tree Learning Summary
BST Definition BST is short for Binary Search Tree, by definition, the value of right node is always ...
- Unique Binary Search Trees [LeetCode]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- Method for balancing binary search trees
Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...
- 算法 binary search
// ------------------------------------------------------------------------------------------------- ...
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- sencha touch list ListPaging使用详解
示例代码: Ext.define('app.view.message.List', { alternateClassName: 'messageList', extend: 'Ext.List', x ...
- TX大手笔做业务必然失败的原因
首先说一个伪命题: 物体会向下落这是一个基本的定律,一个小小的物理规则会覆盖所有物体的行为准则. 那么,当地球上的所有东西都下落的时候,你指望整个地球,月球,太阳也会下落么? 事实上大家都知道星球在宇 ...
- 【BZOJ4361】isn 动态规划+树状数组+容斥
[BZOJ4361]isn Description 给出一个长度为n的序列A(A1,A2...AN).如果序列A不是非降的,你必须从中删去一个数, 这一操作,直到A非降为止.求有多少种不同的操作方案, ...
- oracle如何设置表空间autoextensible自动扩容
SELECT a.tablespace_name "表空间名", total / 1024 / 1024 "表空间大小单位M", free / 1024 / 1 ...
- 最简单,最实用的数据库CHM文档生成工具——DBCHM
DBCHM支持SqlServer/MySql/Oracle/PostgreSQL等数据库的表列批注维护管理. DBCHM有以下几个功能 表,列的批注可以编辑保存到数据库. 表,列的批注支持通过pdm文 ...
- easyui---基础组件:window
window 依赖下面三个组件,就是继承,所以下面的特性和方法 事件都可以用 draggable resizable panel window 和panel不同之处,可以有拖拽移动draggable, ...
- PHPExcel exception: “Could not close zip file … ”报错
Q: PHPExcel exception: “Could not close zip file … ” A:目录没有写权限,chmod 对$phpExcel->save($dir)中报错路径设 ...
- 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表。
https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(详尽版).pdf 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表 ...
- Pragma: no-cache
PHP Advanced and Object-Oriented Programming Larry Ullman Last-Modified 最后修改时间 Expires 过期时间 Pragma ...