Binary Search 的递归与迭代实现及STL中的搜索相关内容
与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search。
首先介绍一下binary search,其原理很直接,不断地选取有序数组的组中值,比较组中值与目标的大小,继续搜索目标所在的一半,直到找到目标,递归算法可以很直观的表现这个描述:
int binarySearchRecursive(int A[], int low, int high, int key)
{
if (low > high) return -;
int mid = (low + high) >> ;
if (key < A[mid]) return binarySearchRecursive(A, low, mid - , key);
else if (key > A[mid]) return binarySearchRecursive(A, mid + , high, key);
else return mid;
}
但实际上,递归方法的时间效率和空间效率都不如迭代方法,迭代方法才是常用的binary search,代码如下:
int binarySearch(int A[], int low, int high, int key)
{
int mid;
while (low <= high)
{
mid = (low + high) >> ;
if (key < A[mid]) high = mid - ;
else if (key > A[mid]) low = mid + ;
else return mid;
}
return -;
}
简单计算一下Binary Search的效率:
算法流程:
1.检查上下边界--2.获取中值--3.比较--左半边进入子问题/右半边进入自问题/获得结果
1,2所需时间为常数时间,设为C。3阶段以一半的数据量重新运行函数,所以:
T(n)=T(n/2)+C
设n=2^k,则有T(2^k)=T(2^(k-1))+C=(T(2^(k-2))+C)+C=T(1)+k*C
即T(n)=log(n)*C+T(1),所以binary search是一个O(log(n))的算法。
测试函数:
void searchTest()
{
int a[] = { ,,,,,,,,,,, };
cout << binarySearch(a, , , ) << endl;
cout << binarySearch(a, , , ) << endl;
cout << binarySearch(a, , , ) << endl;
cout << endl;
cout << binarySearchRecursive(a, , , ) << endl;
cout << binarySearchRecursive(a, , , ) << endl;
cout << binarySearchRecursive(a, , , ) << endl;
}
测试结果如下:
11
-1
-1
11
-1
-1
请按任意键继续. . .
传统C函数中有bsearch这一函数,因为在现代C++中使用C库运行效率很低,加上接口并不好用,不再提及。而STL中,有以下几个关于搜索的函数。他们均作用于各个STL容器。
int count(起始迭代器,终止迭代器,key value)
return key value的数量
iterator find(起始迭代器,终止迭代器,key value)
成功:return 找到的第一个key value的迭代器
失败:return 终止迭代器
bool binary_search(起始迭代器,终止迭代器,key value)
return 是否找到
iterator lower_bound(起始迭代器,终止迭代器,key value)
return 大于或等于key value的第一个迭代器,若所有值都小于key value,返回终止迭代器
iterator upper_bound(起始迭代器,终止迭代器,key value)
return 大于key value的第一个迭代器,若所有值都小于key value,返回终止迭代器
这些函数中,count和find是作用于任意排序对象的,其效率为O(n),而binary_search, lower_bound, upper_bound是作用于有序对象的,其效率是O(logN)。
下面代码给出这些STL函数的测试:
void searchTest()
{
vector<int> b{ ,,,,,,,,,,, };
cout << "vector<int> b{ 1,2,3,4,4,7,9,11,17,20,23,39 };" << endl;
cout << "count(b.begin(), b.end(), 4):"
<< count(b.begin(), b.end(), ) << endl;
cout << endl;
cout << "find(b.begin(), b.end(), 39) - b.begin():"
<< find(b.begin(), b.end(), ) - b.begin() << endl;
cout << "find(b.begin(), b.end(), 4) - b.begin():"
<< find(b.begin(), b.end(), ) - b.begin() << endl;
cout << "find(b.begin(), b.end(), 37) - b.begin():"
<< find(b.begin(), b.end(), ) - b.begin() << endl;
cout << "find(b.begin() + 5, b.begin() + 10, 39) - b.begin():"
<< find(b.begin() + , b.begin() + , ) - b.begin() << endl;
cout << endl;
cout << "binary_search(b.begin(), b.end(), 39):"
<< binary_search(b.begin(), b.end(), ) << endl;
cout << "binary_search(b.begin(), b.end(), 37):"
<< binary_search(b.begin(), b.end(), ) << endl;
cout << endl;
cout << "lower_bound(b.begin(), b.end(), 39) - b.begin():"
<< lower_bound(b.begin(), b.end(), ) - b.begin() << endl;
cout << "lower_bound(b.begin(), b.end(), 4) - b.begin():"
<< lower_bound(b.begin(), b.end(), ) - b.begin() << endl;
cout << "lower_bound(b.begin(), b.end(), 37) - b.begin():"
<< lower_bound(b.begin(), b.end(), ) - b.begin() << endl;
cout << "lower_bound(b.begin() + 5, b.begin() + 10, 39) - b.begin():"
<< lower_bound(b.begin() + , b.begin() + , ) - b.begin() << endl;
cout << endl;
cout << "upper_bound(b.begin(), b.end(), 39) - b.begin():"
<< upper_bound(b.begin(), b.end(), ) - b.begin() << endl;
cout << "upper_bound(b.begin(), b.end(), 4) - b.begin():"
<< upper_bound(b.begin(), b.end(), ) - b.begin() << endl;
cout << "upper_bound(b.begin(), b.end(), 37) - b.begin():"
<< upper_bound(b.begin(), b.end(), ) - b.begin() << endl;
cout << "upper_bound(b.begin() + 5, b.begin() + 10, 39) - b.begin():"
<< upper_bound(b.begin() + , b.begin() + , ) - b.begin() << endl;
}
测试结果:
vector<int> b{ 1,2,3,4,4,7,9,11,17,20,23,39 };
count(b.begin(), b.end(), 4):2
find(b.begin(), b.end(), 39) - b.begin():11
find(b.begin(), b.end(), 4) - b.begin():3
find(b.begin(), b.end(), 37) - b.begin():12
find(b.begin() + 5, b.begin() + 10, 39) - b.begin():10
binary_search(b.begin(), b.end(), 39):1
binary_search(b.begin(), b.end(), 37):0
lower_bound(b.begin(), b.end(), 39) - b.begin():11
lower_bound(b.begin(), b.end(), 4) - b.begin():3
lower_bound(b.begin(), b.end(), 37) - b.begin():11
lower_bound(b.begin() + 5, b.begin() + 10, 39) - b.begin():10
upper_bound(b.begin(), b.end(), 39) - b.begin():12
upper_bound(b.begin(), b.end(), 4) - b.begin():5
upper_bound(b.begin(), b.end(), 37) - b.begin():11
upper_bound(b.begin() + 5, b.begin() + 10, 39) - b.begin():10
请按任意键继续. . .
Binary Search 的递归与迭代实现及STL中的搜索相关内容的更多相关文章
- Binary Search(Java)(递归)
public static int rank(int[] array, int k, int front, int rear) { if(front > rear) return -1; int ...
- 【Leetcode】【Medium】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Trie(前缀树)和ternary trie和binary search tree
1 什么是trie trie是一棵多叉树,假如存放的是由26个字母(不区分大小写)构成的字符串的话,那么就是一棵26叉树. trie树是一棵前缀树,因为每个结点只保存字符串中的一个字符,整个字符串保存 ...
- BINARY SEARCH 的一点说明
在sap 之abap语言中,有BINARY SEARCH这个查找条件.使用read table 来读取内表时,使用BINARY SEARCH可以大大的提高查找的效率,为什么呢?学过数据库的人会知道 ...
- (BST 递归) leetcode98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 二分查找(Binary Search)的递归和非递归
Binary Search 有时候我们也把它叫做二进制查找 是一种较为高效的再数组中查找目标元素的方法 我们可以通过递归和非递归两种方式来实现它 //非递归 public static int bin ...
- Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)
题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
随机推荐
- java 运行环境
1.jre和jdk的区别 jre:java runtime environment, java运行环境,是java程序运行依赖的,包括java的类库的.class文件和kvm. jdk:java de ...
- 《java入门第一季》之Character类小案例
/* * Character 类在对象中包装一个基本类型 char 的值 * 此外,该类提供了几种方法,以确定字符的类别小写字母,数字,等等,并将字符从大写转换成小写,反之亦然 * */ 下面通 ...
- Android ViewManger解析 从ViewRoot 源码分析invalidate
转载请标明出处:http://blog.csdn.net/sk719887916/article/details/48443429,作者:skay 通过学习了AndroidUI之绘图机基础知道 ...
- 【翻译】EXTJS 编码风格指南与实例
原文:EXTJS Code Style Guide with examples Ext JS风格指南: 熟知的且易于学习 快速开发,易于调试,轻松部署 组织良好.可扩展和可维护 Ext JS应用程序的 ...
- Mahout系列之----距离度量
x = (x1,...,xn) 和y = (y1,...,yn) 之间的距离为 (1)欧氏距离 EuclideanDistanceMeasure (2)曼哈顿距离 ManhattanDis ...
- WebService开发指南
WebServiceInAurora Web Service Web Service是一种面向服务的架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作.在Aurora框架 ...
- Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷
Android高级控件(四)--VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷 是不是感觉QQ空间什么的每次新版本更新那炫炫的引导页就特别的激动,哈哈,其实他实现起来真的很简单很 ...
- 携程Android App的插件化和动态加载框架
携程Android App的插件化和动态加载框架已上线半年,经历了初期的探索和持续的打磨优化,新框架和工程配置经受住了生产实践的考验.本文将详细介绍Android平台插件式开发和动态加载技术的原理和实 ...
- ngnix服务器搭建
1. 到nginx官网上下载相应的安装包,http://nginx.org/en/download.html:下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如下图 ...
- 利用可变参实现fprintf函数
#include <stdio.h> #include <stdarg.h> /* 可变参相关接口 typedef char * va_list ; void va_start ...