Binary Search二分法搜索C++程序
二分法基本上学计算机的都听过,但是有人不知道的就是其实二分法是减治法的思想。
所谓减治法和分治法有一个主要差别就是减治法是减去一般,就是分治之后只需要解决原问题的一半就可以了得到全局问题的解了。所以速度很快。
下面是二分法的递归程序和非递归程序和主测试程序:
#include<iostream>
#include<vector>
using namespace std; template<typename T>
int recurBiSearch(const vector<T> &vt, T key, int low, int up)
{
if(low>up) return -1;
int mid = (low+up)>>1;
if (key < vt[mid])
{
return recurBiSearch(vt, key, low, mid-1);
}
else if (vt[mid] < key)
{
return recurBiSearch(vt, key, mid+1, up);
}
return mid;
} template<typename T>
int iterBiSearch(vector<T> &vt, T key, int low, int up)
{
int mid;
while (low<=up)
{
mid = (low+up)>>1;
if(key<vt[mid])
up = mid - 1;
else if(vt[mid]<key)
low = mid + 1;
else return mid;
}
return -1;
} int main()
{
std::vector<int> vec; // set some initial content:
for (int i=1;i<10;i++) vec.push_back(i<<2); vec.resize(7);
vec.resize(12,80); std::cout << "vec contains:";
for (int i=0;i<vec.size();i++)
std::cout << ' ' << vec[i];
std::cout << '\n'; //二分法特征:这里vec.size()-1和不减1都是可以的。
cout<<"Recurrence Search Index Position: ";
int ind = recurBiSearch(vec, 20, 0, vec.size()-1);
cout<<ind;
cout<<"\tValue: "<<vec[ind]<<endl; cout<<"Iterative Search Index Position: ";
ind = iterBiSearch(vec, 20, 0, vec.size()-1);
cout<<ind;
cout<<"\tValue: "<<vec[ind]<<endl; system("pause");
return 0;
}
运行结果:
Binary Search二分法搜索C++程序的更多相关文章
- Binary Search 二分法方法总结
Binary Search 二分法方法总结 code教你做人:二分法核心思想是把一个大的问题拆成若干个小问题,最重要的是去掉一半或者选择一半. 二分法模板: public int BinarySear ...
- 270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点
[抄题]: Given a non-empty binary search tree and a target value, find the value in the BST that is clo ...
- Leetcode之二分法专题-704. 二分查找(Binary Search)
Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 t ...
- Binary Search 的递归与迭代实现及STL中的搜索相关内容
与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search. 首先介绍一下binary search,其原理很直接,不断地选取 ...
- [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
- [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- [Swift]LeetCode700. 二叉搜索树中的搜索 | Search in a Binary Search Tree
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
随机推荐
- firedac调用ORACLE的存储过程
firedac调用ORACLE的存储过程 EMB官方原文地址:http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Using_Oracle_with_F ...
- C#中的Hashtable
richTextBox1.Text = ""; Hashtable ht = new Hashtable(); ht.Add("); ht.Add("); ht ...
- hibernate 注解 联合主键映射
联合主键用Hibernate注解映射方式主要有三种: 第一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将 该类注解 ...
- cocos2d-x基本元素
from://http://www.cnblogs.com/ArmyShen/p/3239664.html 1.CCDirector(导演类) 控制游戏流程的主要类,主要负责设定游戏窗口.切换场景.暂 ...
- Netty4.0学习笔记系列之四:混合使用coder和handler
Handler如何使用在前面的例子中已经有了示范,那么同样是扩展自ChannelHandler的Encoder和Decoder,与Handler混合后又是如何使用的?本文将通过一个实际的小例子来展示它 ...
- log4j修改SMTPAppender支持ssl
- byte数组怎么存放到Json中传递
可以把byte[]序列化成base64字符串后,再放json里传输就可以了.不需要考虑每个字节转成一个字符存到json字符串里. String str = Base64.encodeToString( ...
- 绝命毒师第一季/全集Breaking Bad迅雷下载
本季Breaking Bad Season 1(2008)看点:新墨西哥州的高中化学老师沃尔特·H·怀特(布莱恩·科兰斯顿 Bryan Cranston 饰)是拮据家庭的唯一经济来源.他大半生安分守己 ...
- 开源项目PullToRefresh详解(一)——PullToRefreshListView
开源项地址:https://github.com/chrisbanes/Android-PullToRefresh 下拉刷新这个功能我们都比较常见了,今天介绍的就是这个功能的实现.我将按照这个开 ...
- Java中的线程实现
线程实现有两种方法: 1.写一个类来继承Thread类,然后复写run()方法. public class HelloThread extends Thread { public void run() ...