Examples of complexity pattern
O(1):constant - the operation doesn't depend on the size of its input, e.g. adding a node to the tail of a linked list where we always maintain a pointer to the tail node.
int i=0;
i++;
++i;
i+=6;
O(n):linear - the run time complexity is proportionate to the size of n.
int i,n=100,s=0;
for(i=0;i<n;i++)
{
s+=1;
}
O(n2):quadratic - the run time complexity is proportionate to the square of size of n, e.g., bubble sort.
int i,j,n=100,s=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
s+=1;
}
}
O(n3):cubic - very rare.
int i,j,k,n=100,s=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
s+=1;
}
}
}
O(logmn): logarithmic: normally associated with algorithms that break the problem into smaller chunks per each invocation, e.g. searching a binary search tree.
int i,n=100,m=2; /* m could be any number, e.g.,2,10 */
for(i=0;i<n;i++)
{
i=i*m;
}
O(nlogn): just nlogn: usually associated with an algorithm that breaks the problem into smaller chunks per each invocation, and then takes the results of these smaller chunks and stitches them back together, e.g. quick sort.
int i,n=100;
int m_expo(int m)
{
/* an auxilary function that return the value
of m to the mth exponential, not included to the
time consumation*/
int k = m;
for(j=1;j<m;j++)
{
/* 2 could also be other number */
k = k * k;
}
return k;
}
/* this is the part whose consumation is O(nlogn) */
for(i=0;i<m_expo(n);i++)
{
/* 10 could be other number */
i=i*10;
}
O(n1/2): square root.
int i,n=100;
while(i*i<n)
{
i++;
}
O(2n):exponential - incredibly rare.
int i,n=100;
int expo(int m)
{
/* an auxilary function that return the value
of 2 to the mth exponential, not included to the
time consumation*/
int k =1;
for(j=1;j<m;j++)
{
/* 2 could also be other number */
k = k * 2;
}
return k;
}
/* this is the part whose consumation is O(2n)) */
while(i<expo(n))
{
i++;
}
O(n!):factorial - incredibly rare.
int i,n=100;
int factorial(int m)
{
/* an auxilary function that return the
factorial value of m */
int k =1;
for(j=1;j<=m;j++)
{
/* 2 could also be other number */
k = j * k;
}
return k;
}
/* this is the part whose consumation is O(n!) */
while(i<factorial(n))
{
i++;
}
O(nn):not exist in real life.
int i,n=100;
int mm_expo(int m)
{
/* an auxilary function that return the value
of m to the mth exponential, not included to the
time consumation*/
int k = m;
for(j=1;j<m;j++)
{
k = k * m;
}
return k;
}
/* this is the part whose consumation is O(nn)) */
while(i<mm_expo(n))
{
i++;
}
Examples of complexity pattern的更多相关文章
- Observer Pattern
Motivation We can not talk about Object Oriented Programming without considering the state of the ob ...
- A Pattern Language for Parallel Application Programming
A Pattern Language for Parallel Application Programming Berna L. Massingill, Timothy G. Mattson, Bev ...
- 脚本AI与脚本引擎
Scripted AI and Scripting Engines 脚本AI与脚本引擎 This chapter discusses some of the techniques you can us ...
- linq 动态组合条件
http://www.albahari.com/nutshell/predicatebuilder.aspx Dynamically Composing Expression Predicates S ...
- Java内部类、静态嵌套类、局部内部类、匿名内部类
Nested classes are further divided into two types: static nested classes: If the nested class is sta ...
- Python中的正则表达式regular expression
1 match = re.search(pat,str) If the search is successful, search() returns a match object or None o ...
- DescribingDesign Patterns 描述设计模式
DescribingDesign Patterns 描述设计模式 How do we describe design patterns?Graphical notations, while impor ...
- 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)
原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
- A brief introduction to weakly supervised learning(简要介绍弱监督学习)
by 南大周志华 摘要 监督学习技术通过学习大量训练数据来构建预测模型,其中每个训练样本都有其对应的真值输出.尽管现有的技术已经取得了巨大的成功,但值得注意的是,由于数据标注过程的高成本,很多任务很难 ...
随机推荐
- shared_ptr:资源管理利器
如果你还在使用传统的C++,那么可以肯定堆内存的管理让你头痛过!在传统的C++领域,堆内存管理上我们能借用的现成工具就只有auto_ptr.但是很不幸用auto_ptr管理堆内存简直就是个错误.aut ...
- JQ对象到底是什么
jQuery对象是什么,举个例子,$('#id') 返回的就是jQuery对象,这个东西是整个jQuery的核心所在,所以我先来分析它. var jQuery = function( selector ...
- Python的平凡之路(19)
一.Django请求生命周期 对于所有的web框架来说本质就是一个socket服务端,浏览器是socket客户端 ...
- opacity与RGBA透明的区别
为什么不使用opacityCSS3 还允许通过opacity 声明来设置元素的透明度.该透明度的值也是一个介于0 到1 之间的小数(如将opacity 设置为0.1 表示为10%透明).但是这种透明度 ...
- 布局容器layout Container
画布canvas 盒子Box VBox Hbox-->HGroup VGroup 控制条 ControlBar
- Thinkphp模型问题(一)
很长一段时间,脑海里没有模型和控制器的区别,几乎把所有代码都敲在控制器里边了. 来自于ThinkPhp3.2.3手册模型定义 模型类的作用大多数情况是操作数据表的,如果按照系统的规范来 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
- 每次Xcode 升级之后 插件失效,两步解决
以下内容来源:http://www.cocoachina.com/bbs/read.php?tid=296269 每次Xcode 升级之后 插件失效,两步解决 1.打开终端,输入以下代码获取到DVTP ...
- ZendStudio13 PHP调试环境快速配置
1.百度ZendStudio13,汉化,破解,注册: 2.安装国产Apache+MySql一键安装环境phpStudy,方便快速 http://www.phpstudy.net/a.php/208. ...
- 使用递归方法遍历TreeView的节点如配置则勾选
自己在网上找了一段时间都没有找到取自数据库的值与TreeView中的节点值进行对比如匹配则勾选中CheckBox的资料,后面自己写了一个递归方法实现,现在记录起来以备需要时使用.在没写这递归方法前如果 ...