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 南大周志华 摘要 监督学习技术通过学习大量训练数据来构建预测模型,其中每个训练样本都有其对应的真值输出.尽管现有的技术已经取得了巨大的成功,但值得注意的是,由于数据标注过程的高成本,很多任务很难 ...
随机推荐
- 与焊接厂交流——从生产角度出发的PCB设计心得
上周的时候,去了趟加工厂盯电路板的焊接进度.然后在闲余的时候,跟焊接厂的工程师交流了一下,工程师从生产的角度,说了几个值得注意的事项: 1.元件的焊盘应该要窄长,不能过宽.因为,在过机表贴时,焊盘上的 ...
- 生成模型(Generative)和判别模型(Discriminative)
生成模型(Generative)和判别模型(Discriminative) 引言 最近看文章<A survey of appearance models in visual object ...
- JAVA第四周作业
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...
- jQuery原生框架-----------------属性操作
// 添加一个处理兼容获取样式的静态方法jQuery.getStyle = function( dom, styleName ) { // dom不是dom,styleName不是字符串,直接打走 i ...
- spring aop 声明式事务管理
一.声明式事务管理的概括 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的方式之一. Spring的声明式事务顾名思义就是采用声明 ...
- Android开源框架:Universal-Image-Loader解析(三)DiskCache
- CSS的三种引入方式
1.标签 <style>定义样式 <link> 资源的引用(某些css的样式) 2.属性 rel=”stylessheet” 外部样式表 (rel为定义当前文 ...
- jquery.hover()函数使用
<script> $(document).ready(function (t) { $(".wadver li").hover(function(event) { $( ...
- 【杂】孔明锁6根解法 & 九连环的拆卸方法及还原
**************************** Part1: 孔明锁6根解法: **************************** 第一步,编号: 第二步,按照编号组装: 第三步,完成 ...
- Java记事本编译
配置环境: 在“系统变量”栏下执行三项操作:①新建“Java_Home”,设置其值为 JDK所在的绝对路径,如果你的事刚才的默认路径,那值为:C:\Program Files\Java\jdk1.7. ...