C++11写算法之顺序查找
从这篇博文起,将尝试使用C++11来写常用算法与数据结构。
本篇博文以最简单的顺序查找作为系列博文的起点,并作约定如下:
1,变量名 : varList ; 函数名 : SequentialFind ;
2,尽量描写算法本身,因而均不含模板,数据类型均为int;
3,所有代码均在同一个cpp中;
4,代码均在 vs2013 与 g++ 4.8.2 中编译通过;
5,g++编译命令:g++ -std=c++11 sequential_find.cpp 。
顺序查找没什么好说的,这里主要看看C++11的lambda,auto新语义。
时间复杂度:O(n)
空间复杂度:O(1)
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; int SequentialFind(const int varList[], const int size, const int target)
{
if (!varList || size < )
{
return -;
}
int index = -;
for (int i = ; i < size;i++)
{
if (varList[i] == target)
{
index = i;
break;
}
}
return index;
} void test()
{
//case counter
int testCase = ;
//find function object
auto findFunc = SequentialFind;
//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写算法之二分查找
同样的,二分查找很好理解,不多做解释,要注意二分查找的list必须是排好序的. 这里实现了两种二分查找的算法,一种递归一种非递归,看看代码应该差不多是秒懂.想试验两种算法,改变一下findFunc函数 ...
- C语言查找算法之顺序查找、二分查找(折半查找)
C语言查找算法之顺序查找.二分查找(折半查找),最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 顺序查找 /*顺序查找 顺序查找是在一个已知无(或有序)序队列中找出与给定关键字相同的 ...
- Java中的查找算法之顺序查找(Sequential Search)
Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...
- javascript数据结构与算法---检索算法(顺序查找、最大最小值、自组织查询)
javascript数据结构与算法---检索算法(顺序查找.最大最小值.自组织查询) 一.顺序查找法 /* * 顺序查找法 * * 顺序查找法只要从列表的第一个元素开始循环,然后逐个与要查找的数据进行 ...
- 数据结构与算法之PHP查找算法(顺序查找)
对于查找数据来说,最简单的方法就是从列表的第一个元素开始对列表元素逐个进行判断,直到找到了想要的结果,或者直到列表结尾也没有找到,这种方法称为顺序查找. 一.基本写法 顺序查找的实现很简单.只要从列表 ...
- 查找算法(顺序查找、二分法查找、二叉树查找、hash查找)
查找功能是数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数组中没有重复的数据存在. (1)顺 ...
- C++11写算法之插入排序
插入排序,是指将从1 –> size-1的数一个个插入到前面已经排序好的数组中. 时间复杂度:O(n^2) , O(nlgn) (lgn指使用二分查找插入点位置) 空间复杂度:O(1) // ...
- C++11写算法之选择排序
选择排序,顾名思义,指从数组后面将最小的值找出来,然后与最前面(指当前位置)值进行交换. 时间复杂度:O(n^2) 空间复杂度:O(1) 此处应用了C++11的auto , lambda , stat ...
- C++11写算法之冒泡排序
冒泡排序很形象,指从数组后面将更小的值慢慢浮到前面去,每遍历一趟使得最小值浮到最前面(指当前位置). 这里有点小技巧,当某一次遍历过程中发现无交换,则说明此时数组已经排序完成,可提前退出. 时间复杂度 ...
随机推荐
- Windows 2003 R2
微软发布Windows Server 2003 R2版的目的是希望透过它填补Windows Server 2003 SP1和Longhorn Server之间的产品发布时间间隔. 微软向产品测试人员表 ...
- window安装mysql5.7.11
1.到mysql官网(http://dev.mysql.com/downloads/mysql/)下载压缩包,我的是win7 64位的,根据自己的系统进行下载 2.解压到自己的目录,我的是 E:\so ...
- e.which
e.which is not an event, which is a property of the event object, which most people label as e in th ...
- (草稿)spring @value 原理源码解读
一切要从这说起:http://www.cnblogs.com/guazi/p/6698654.html 我们直接开始debug: 这里会遍历所有的需要注入的InjectedElement 这里我们需要 ...
- linux下confstr与uname函数_获取C库与内核信息
#include <stdio.h> #include <sys/utsname.h> //uname int main(int argc, char **argv[]) { ...
- Vue 状态管理 Vuex
1.概述 Vuex作为插件,管理和维护整个项目的组件状态. 2.安装vuex cnpm i --save vuex 3.vuex使用 github地址:https://github.com/MengF ...
- zoj 2744 - Palindromes
题目:统计一个串中的回文子串的个数(注意是子串,要连续). 分析:dp.暴力.直接用dp,二维数组内存不够用,并且dp木有暴力快( ⊙ o ⊙ )啊! 说明:(2011-09-24 03:22). # ...
- Android编程之常识 - 混淆
1,什么是混淆编译 ProGuard是一个免费的java类文件压缩,优化,混淆器.它探测并删除没有使用的类,字段,方法和属性.它删除没有用的说明并使用字节码得到最大优化.它使用无意义的名字来重命名类, ...
- LeetCode——Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- JDK自带监控工具 jps、jinfo、jstat、jmap、jconsole
分类: JVM 2010-10-04 11:05 587人阅读 评论(0) 收藏 举报 工具jdkjava远程连接unixstring 常用有五个命令行工具: jinfo: 可以输出并修改运行时的ja ...