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 ...
随机推荐
- Strategy 设计模式 策略模式 超靠谱原代码讲解
先来假设一种情,我们需要向三种不同的客户做出不同的报价,一般来说要肿么设计呢,是不是马上会想到用IF,没有错,对于这种情况,策略模式是最好的选.大家可以这么理解,如果有情况需要用到大量的IF,那你用策 ...
- 中国梦 每个农大人的梦
历经百年风霜,苦经岁月沧桑.农大,一个中原沃土上生长起来的大树,它在用它那不倒的生命力展示着农大的顽强与坚持,而这份苍劲和顽强,却来自于每个农大人,来自于他们的梦想,来自于他们的坚持,来自于他们的努力 ...
- 【翻译】使用Ext JS设计响应式应用程序
原文:Designing Responsive Applications with Ext JS 在当今这个时代,用户都希望Web应用程序无论在形状还是大小上,既能在桌面电脑,也能在移动设备上使用.使 ...
- 《java入门第一季》之StringBuffer小案例
这里是针对其反转功能来举的例子,再对比之前写的一篇String类的反转功能,StringBuffer明显提高了代码量,提高了效率. import java.util.Scanner; /* * 把字符 ...
- 嵌入式C语言查表法的项目应用
嵌入式C实战项目开发技巧:如果对一个有规律的数组表进行位移操作 就像下面的这个表 之前写过上面这个标题的一篇文章,讲的是以位移的方式去遍历表中的数据,效率非常高,但是,如果要实现一个乱序的流水灯或者跑 ...
- SlopOne 改进
lope One 其基本的想法来自于简单的一元线性模型 $w = f(v) = v + b$.已知一组训练点 ${(v_i, w_i)}_{i=1}^n$,利用此线性模型最小化预测误差的平方和,我们可 ...
- windows linux—unix 跨平台通信集成控制系统
首先,我们可以用到这个开源的开发包: mdk(Micro-Development-Kit)微量级软件开发包,提供几个常用类,主要实现了一个高性能的并发服务器引擎 使用c++开发,是一个跨平台的开发包, ...
- C++之默认参数
C++可以为不指定参数提供默认值.一旦给一个参数赋了默认值,后面的所有参数,也都必须为默认值,并且默认值的类型也必须正确,默认值可以在原型或者函数定义中给出,但是不能两个位置同时给出. 接下来我们上代 ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- 如何使用VS2013本地C++单元测试框架
在VS2013中,可以使用VS自带的C++单元测试框架. 在使用该框架前,需要先安装Unit Test Generator(可以通过菜单“工具->扩展和更新”搜索安装). 下边,就阐述一下利用该 ...