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 ...
随机推荐
- Linux GPRS模块问题
这是一个硬件问题,不过被我这个学软件的给遇到了.很尴尬,纠结了很久. GPRS模块如果没有插上sim卡,开机之后一切正常.一旦插上卡之后开机大约过十秒钟之后会自动关机.并在串口上面打印一下信息: II ...
- http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SVYGqy364vDfv6AY4ERPa
http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SV ...
- mapreduce实现搜索引擎简单的倒排索引
使用hadoop版本为2.2.0 倒排索引简单的可以理解为全文检索某个词 例如:在a.txt 和b.txt两篇文章分别中查找统计hello这个单词出现的次数,出现次数越多,和关键词的吻合度就越高 现有 ...
- Leagal or Not —— 拓扑排序(王道)
Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...
- 【招聘App】—— React/Nodejs/MongoDB全栈项目:登录注册
前言:最近在学习Redux+react+Router+Nodejs全栈开发高级课程,这里对实践过程作个记录,方便自己和大家翻阅.最终成果github地址:https://github.com/66We ...
- Java基础——Statement与PrepareStatement
Statement Statement是Java运行数据库操作的一个重要方法.用于在已经建立数据库连接的基础上.向数据库发送要运行的SQL语句.Statement对象,用于运行不带參数的简单SQL语句 ...
- C#秘密武器之泛型
一.简介: 很多初学者在刚开始接触泛型的时候会比较难理解泛型,在这里先把 “泛型”当作一个形容词,这样就方便理解了,因为很多东西都可以是泛型的!比如:“泛型的类”,“泛型的方法”,“泛型的接口”,“泛 ...
- 用Volley-nullpointerexception
public Request(int method, String url, Response.ErrorListener listener) { mMethod = method; mUrl = u ...
- 通过ngxtop实时监控webserver的访问情况 / 解决ImportError: No module named _sqlite3问题
通过ngxtop实时监控webserver的访问情况 2014-04-03 0个评论 来源:通过ngxtop实时监控web server的访问情况 收藏 我要投稿 关于对ng ...
- MapReduce源代码分析之JobSubmitter(一)
JobSubmitter.顾名思义,它是MapReduce中作业提交者,而实际上JobSubmitter除了构造方法外.对外提供的唯一一个非private成员变量或方法就是submitJobInter ...