二分法基本上学计算机的都听过,但是有人不知道的就是其实二分法是减治法的思想。

所谓减治法和分治法有一个主要差别就是减治法是减去一般,就是分治之后只需要解决原问题的一半就可以了得到全局问题的解了。所以速度很快。

下面是二分法的递归程序和非递归程序和主测试程序:

#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++程序的更多相关文章

  1. Binary Search 二分法方法总结

    Binary Search 二分法方法总结 code教你做人:二分法核心思想是把一个大的问题拆成若干个小问题,最重要的是去掉一半或者选择一半. 二分法模板: public int BinarySear ...

  2. 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 ...

  3. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

  4. Binary Search 的递归与迭代实现及STL中的搜索相关内容

    与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search. 首先介绍一下binary search,其原理很直接,不断地选取 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. SQLCE使用本地数据库优化

    一.数据绑定 1.使用数据虚拟化和SKIP/TAKE 使用 Skip 和 Take 方法可确保直到需要在 ListBox 控件中显示数据时才将数据库中的数据加载到内存中. 例如,以下代码显示了如何从数 ...

  2. div与span区别及用法

    DIV与SPAN区别及div与san用法篇 接下来了解在div+css开发的时候在html网页制作,特别是标签运用中div和span的区别及用法.新手在使用web标准(div css)开发网页的时候, ...

  3. 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示

    我们都知道在ios中,每一个UIImageView都有他的frame大小,但是如果图片的大小和这个frame的大小不符合的时候会怎么样呢?在默认情况,图片会被压缩或者拉伸以填满整个区域. 通过查看UI ...

  4. 【mybatis】mybatis进行批量更新,报错:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right

    使用mybatis进行批量更新操作: 报错如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an erro ...

  5. ASP.NET MVC:@helper 不能调试

    ASP.NET MVC 的 @helper 不能设置断点,当然我们可以将逻辑移动到扩展方法中,这里介绍另外一种方式,使用:System.Diagnostics.Debug.WriteLine,编程旅途 ...

  6. 使用DDMS中的内存监测工具Heap来优化内存

    最近在做一个照片墙的应用,涉及到很多知识,其中难点在于如何应对数量庞大的图片,这就涉及到内存管理的知识了.今天介绍的工具是DDMS中自带的Heap,它可以显示出当前引用占用的内存,剩余的内存等信息.下 ...

  7. 详细解读Android中的搜索框(一)—— 简单小例子

    这次开的是一个讲解SearchView的栏目,第一篇主要是给一个小例子,让大家对这个搜索视图有一个了解,之后再分布细化来说. 目标: 我们先来定个目标,我们通过搜索框来输入要搜索的联系人名字,输入的时 ...

  8. 彻底理解Java的feature模式

    先上一个场景:假如你突然想做饭,但是没有厨具,也没有食材.网上购买厨具比较方便,食材去超市买更放心. 实现分析:在快递员送厨具的期间,我们肯定不会闲着,可以去超市买食材.所以,在主线程里面另起一个子线 ...

  9. probotuf 标量数值类型

    标量数值类型 一个标量消息字段可以含有一个如下的类型--该表格展示了定义于.proto文件中的类型,以及与之对应的.在自动生成的访问类中定义的类型: .proto类型 Java 类型 C++类型 备注 ...

  10. verilog语法实例学习(8)

    常用的时序电路介绍 在电平敏感的锁存器时钟信号有效期(高电平)期间,锁存器的状态随着输入信号的变化而变化.有时候,我们需要存储器的状态在一个时钟周期只改变一次,这个时候就用到了触发器.触发器(flip ...