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 ...
随机推荐
- gulp生成发布包脚本
var formPost = require('./tools/submit.js');var gulp = require('gulp'), zip = require('gulp-zip'), h ...
- win10 与linux mint双系统 只能进入mint而无法进入windows的解决方案
新购买了一块ssd,和以前的hdd硬盘一起装双系统:win10和mint ssd:win10 sdb1 sdb2 sdb3 sda2 hdd: mint sda1 ...
- myisam与innodb索引比较
MyISAM支持全文索引(FULLTEXT).压缩索引,InnoDB不支持 InnoDB支持事务,MyISAM不支持 MyISAM顺序储存数据,索引叶子节点保存对应数据行地址,辅助索引很主键索引相差无 ...
- iOS property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的. 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都 ...
- JDBC改进版
将setObject隐藏,用反射获取model里面的数据 /** * @Date 2016年7月19日 * * @author Administrator */ package com.eshore. ...
- wpgcms---首页数据怎么掉
在首页调用单页模型下的,单页列表使用的方法: {% for page in wpg.page.getList('business') %} <li> <div class=" ...
- openstack 部署(Q版)-----keystone认证服务安装配置
一.新建数据库及用户 CREATE DATABASE keystone; GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' ID ...
- Mysql数据库操作复习,增删改查
Mysql数据库 Apache(服务员) php(大厨) mysql(冰柜) Mysql是瑞典的mysqlAB公司开发的一款中小型关系型数据库管理系统. MysqlAB公司在2008年被S ...
- jquery给动态添加的dom元素绑定事件
$('input').click(function () { //处理代码 }); 这只能是对已经加载好的元素定义事件,那些后来添加插入的元素则需要另行绑定,对于页面中动态添加的元素,在页面加载完成后 ...
- NLP常用语料集合
常用语料资源 下面提供一些网上能下载到的中文的好语料,供研究人员学习使用.(1).中科院自动化所的中英文新闻语料库 http://www.datatang.com/data/13484中文新闻分类语料 ...