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 南大周志华 摘要 监督学习技术通过学习大量训练数据来构建预测模型,其中每个训练样本都有其对应的真值输出.尽管现有的技术已经取得了巨大的成功,但值得注意的是,由于数据标注过程的高成本,很多任务很难 ...
随机推荐
- hihoCoder#1039
刚开始学习C语言,准备在做hiho的题目的过程中来学习,在此进行记录,如果代码中有错误或者不当的地方还请指正. 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在 ...
- strace命令使用
在调试的时候,strace能帮助你追踪到一个程序所执行的系统调用.当你想知道程序和操作系统如何交互的时候,这是极其方便的,比如你想知道执行了哪些系统调用,并且以何种顺序执行. 这个简单而又强大的工具几 ...
- css 中content内容特殊形状
用到的一些特殊字符和图标html代码<div class="cross"></div>css代码.cross{ width: 20px; hei ...
- EasyUI使用JSON保存数据
目前来说,使用JSON保存数据比较方便,前台可以不用Test.aspx 页面,可以直接用Html页面,使用.aspx页面的弊端就不在这里熬述. 具体步骤如下: 1.新建一个Html页面,命名为Test ...
- 细说JAVA反射
Reflection 是 Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说“自审”,并能直接操作程序的内部属性.例如,使用它能获得 Java 类中各成员的名称并显 ...
- iOS学习之MVC模式
Modal 模型对象: 模型对象封装了应用程序的数据,并定义操控和处理该数据的逻辑和运算.例如,模型对象可能是表示商品数据 list.用户在视图层中所进行的创建或修改数据的操作,通过控制器对象传达出去 ...
- cocos2dx-3.9 集成admob
Part 1: 安装GoogleMobileAds framework (即admob) 1. 安装Cocoapods,否则解决依赖关系和配置真的会把人不累死也得烦死 sudo gem install ...
- php 队列
一.php中对共享内存,消息队列的操作 http://blog.csdn.net/haitun312366/article/details/8614797 二.PHP memcache 队列类 htt ...
- HTML5新增元素
由于DIV的滥用,导致页面结构混乱,所以,与HTML4相比,HTML5主要新增了结构元素 新增的元素 1.结构元素: section 表示页面中的一个块或者章节,比如页眉.页脚,或者页面中的其它部分 ...
- UE4 Android打包 问题 记录笔记
问题一:error: expression result unused [-Werror,-Wunused-value] 虽然看了输出日志知道了这行沉余代码删掉就行,但是不是很懂这个地方报错意义. 问 ...