C++11写算法之二分查找
同样的,二分查找很好理解,不多做解释,要注意二分查找的list必须是排好序的。
这里实现了两种二分查找的算法,一种递归一种非递归,看看代码应该差不多是秒懂。想试验两种算法,改变一下findFunc函数指针(auto findFunc = RecursionBinaryFind; //BinaryFind )即可。
时间复杂度:O(lgn)
空间复杂度:O(1)
除了顺序查找和二分查找,还有一些需要借助某些数据结构才能进行查找的算法,例如:分块查找,二叉排序树查找,哈希查找,B树/B+树/B*树查找等,这些算法的重点更偏向于数据结构本身,因而将放在数据结构部分进行示例。
show me the code !
// #if __cplusplus < 201103L
// #error "must be compiled under c++11 support platform!!!"
// #endif
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cassert>
using namespace std; //WARNING : input varList of function RecursionBinaryFind must be sorted!!!
int RecursionBinaryFindImp(const int varList[], const int begin,const int end, const int target)
{
if (!varList || begin > end)
{
return -;
}
int index = -;
int mid = (begin + end) >> ; if (target == varList[mid])
{
index = mid;
}
else if (target < varList[mid])
{
index = RecursionBinaryFindImp(varList,begin,mid - ,target);
}
else if (target > varList[mid])
{
index = RecursionBinaryFindImp(varList, mid + , end, target);
} return index;
}
int RecursionBinaryFind(const int varList[], const int size, const int target)
{
if (!varList || size < )
{
return -;
}
return RecursionBinaryFindImp(varList,,size-,target);
} //WARNING : input varList of function SequentialFind must be sorted!!!
int BinaryFind(const int varList[], const int size, const int target)
{
if (!varList || size < )
{
return -;
}
int index = -;
int begin = ;
int end = size - ;
int mid = (begin + end) >> ;
while (begin<end)
{
if (target == varList[mid])
{
break;
}else if (target < varList[mid])
{
end = mid - ;
}else if (target > varList[mid])
{
begin = mid + ;
}
mid = (begin + end) >> ;
}
index = mid;
return index;
} void test()
{
//case counter
int testCase = ;
//find function object
auto findFunc = RecursionBinaryFind; //BinaryFind
//show case result lambda function
auto showFunc = [&testCase](){cout << "case[" << testCase++ << "] ok... "<<endl; }; cout << "test begin : " << endl << endl; //case empty list
{
assert(- == findFunc(nullptr, , ));
showFunc();
}
//case wrong list size
{
const int testList[] = { -, -, , , , , , , };
assert(- == findFunc(testList, , ));
showFunc();
}
//case not found
{
const int testList[] = { -, -, , , , , , , };
const int size = sizeof(testList) / sizeof(int);
const int target = -;
assert(- == findFunc(testList, , ));
showFunc();
}
//case found at begin position
{
const int testList[] = { -, -, , , , , , , };
const int size = sizeof(testList) / sizeof(int);
const int target = -;
assert( == findFunc(testList, size, target));
showFunc();
}
//case found at random position
{
const int testList[] = { -, -, , , , , , , };
const int size = sizeof(testList) / sizeof(int);
const int target = ;
assert( == findFunc(testList, size, target));
showFunc();
}
//case found at end position
{
const int testList[] = { -, -, , , , , , , };
const int size = sizeof(testList) / sizeof(int);
const int target = ;
assert(size - == findFunc(testList, size, target));
showFunc();
} cout <<endl<< "test done ! " << endl << endl;
} int main(int argc, char* argv[])
{
test();
return ;
}
C++11写算法之二分查找的更多相关文章
- C++11写算法之顺序查找
从这篇博文起,将尝试使用C++11来写常用算法与数据结构. 本篇博文以最简单的顺序查找作为系列博文的起点,并作约定如下: 1,变量名 : varList : 函数名 : SequentialFind ...
- 分治算法(二分查找)、STL函数库的应用第五弹——二分函数
分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...
- 【算法】二分查找法&大O表示法
二分查找 基本概念 二分查找是一种算法,其输入是一个有序的元素列表.如果要查找的元素包含在列表中,二分查找返回其位置:否则返回null. 使用二分查找时,每次都排除一半的数字 对于包含n个元素的列表, ...
- javascript数据结构与算法---检索算法(二分查找法、计算重复次数)
javascript数据结构与算法---检索算法(二分查找法.计算重复次数) /*只需要查找元素是否存在数组,可以先将数组排序,再使用二分查找法*/ function qSort(arr){ if ( ...
- python算法之二分查找
说明:大部分代码是在网上找到的,好几个代码思路总结出来的 通常写算法,习惯用C语言写,显得思路清晰.可是假设一旦把思路确定下来,并且又不想打草稿.想高速写下来看看效果,还是python写的比較快.也看 ...
- JS算法之二分查找
二分查找法主要是解决「在一堆有序的数中找出指定的数」这类问题,不管这些数是一维数组还是 多维数组,只要有序,就可以用二分查找来优化. 二分查找是一种「分治」思想的算法,大概流程如下: 1.数组中排在中 ...
- Java查找算法之二分查找
二分查找是一种查询效率非常高的查找算法.又称折半查找. 一.算法思想 有序的序列,每次都是以序列的中间位置的数来与待查找的关键字进行比较,每次缩小一半的查找范围,直到匹配成功. 一个情景:将表中间位置 ...
- 算法入门——二分查找,旅行商问题,大O表示法
一. 算法入门 博主在市面上发现了很多,很多有关书算法的书籍,但是真正能够让初学者易懂的算法书籍,只是一点点,以下我讲以 Aditya Bhargava写的一本关于算法的入门书籍,为参考,这本书非常的 ...
- 算法:二分查找(python版)
#!/usr/bin/env python #coding -*- utf:8 -*- #二分查找#时间复杂度O(logn)#一个时间常量O(1)将问题的规模缩小一半,则O(logn) import ...
随机推荐
- 好未来AI Lab 思考下面的问题
好未来AI Lab和科赛联合举办的TAIL CAMP——AI实战训练营 图像识别: 卷积层是所有CNN网络中必不可少的模块,请解释为什么3X3的卷积是最为常用的卷积核大小?小尺寸卷积核(1x1)和大尺 ...
- python 操作solr索引数据
测试代码1: def test(self): data = {", "*字段名*": u"我是一个大好人"}}} params = {"bo ...
- JNI之——在cmd命令行下编译执行C/C++源文件
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46604269 一直用java来敲代码,java配置好jre路径之后.在cmd下编译 ...
- selenium以手机模拟器方式打开Google浏览器
使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码 public class runtest { ...
- docker集群——介绍Mesos+Zookeeper+Marathon的Docker管理平台
容器为用户打开了一扇通往新世界的大门,真正进入这个容器的世界后,却发现新的生态系统如此庞大.在生产使用中,不论个人还是企业,都会提出更复杂的需求.这时,我们需要众多跨主机的容器协同工作,需要支持各种类 ...
- 在FASTBuild中使用Caching
上一篇:初识FASTBuild 在FASTBuild中使用缓存只需要注意三个环节: 一.设置编译选项 对于GCC\SNC\Clang编译器,没有特殊的要求 对于MSVC编译器,必须设置/Z7调试模式. ...
- S6:组合模式 Composite
将对象组合成树形结构以表示整体-部分的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. UML: 示例代码:透明组合:叶节点和子节点具有相同的接口 abstract class Com ...
- Hybrid App开发实战
Hybrid App开发实战 作者 李秉骏 发布于 九月 04, 2013 | [引言]近年来随着移动设备类型的变多,操作系统的变多,用户需求的增加,对于每个项目启动前,大家都会考虑到的成本,团队成员 ...
- EXTJS4自学手册——图形行为(rotate,scale)
一.旋转图像: { xtype: 'button', text: '旋转的字', handler: function (btn) { Ext.create('Ext.window.Window', { ...
- Note:pandas时间序列处理
Note 1.Time Series #1 from datetime import datetime now=datetime.now() now.year now.month now.day #2 ...