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写算法之冒泡排序
冒泡排序很形象,指从数组后面将更小的值慢慢浮到前面去,每遍历一趟使得最小值浮到最前面(指当前位置). 这里有点小技巧,当某一次遍历过程中发现无交换,则说明此时数组已经排序完成,可提前退出. 时间复杂度 ...
随机推荐
- 处理 WebService 中的 Map 对象
最近,我们讨论了关于 WebService 的相关问题.目前在 Smart 中,可发布两种类型的 WebService,它们是:SOAP 服务 与 REST 服务,您可以根据需要自由选择. 今天,我要 ...
- D3.js系列——布局:弦图和集群图/树状图
一.弦图 1.弦图是什么 弦图(Chord),主要用于表示两个节点之间的联系的图表.两点之间的连线,表示谁和谁具有联系. 2.数据 初始数据为: var city_name = [ "北京& ...
- @CrossOrigin 跨域注解
在spring 4.2后,提供了跨域注解@CrossOrigin https://spring.io/guides/gs/rest-service-cors/ Enabling CORS Contro ...
- HTML5 Canvas 绘制库存变化折线 增加超储告罄线
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- Android中Service概述
Service是Android中一种非常重要的组件,一般来说有两种用途:用Service执行长期执行的操作,而且与用户没有UI界面的交互:某个应用程序的Service能够被其它应用程序的组件调用以便提 ...
- node.js开发平台
1.EDP:基于Node.JS与NPM的企业级开发平台 什么是EDP? EDP是一个基于Node.JS与NPM的企业级前端应用的开发平台.主要通过命令行的方式使用.EDP提供了前端应用开发时经常使用的 ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- jQuery-DesktopGrid
jQueryDesktopGrid jQueryDesktopGrid migrate to https://github.com/jelly-liu/jquery-osx jQuery deskto ...
- ajax乱码解决总结
第一,javascript沿用java的字符处理方式,内部是使用unicode来处理所有字符的,第二,utf-8是每个汉字(unicode字符)用3个字节来存储.第三,用utf-8来send数据是不会 ...
- 转MQTT SERVER 性能测试报告
硬件环境: 内存4G CPU4核 SERVER及端口: apollo端口 61619 mosquitto:端口 1884 activeMQ端口:1883 emqtt 端口1885 测试方法 并发测试: ...