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++;
}

时间: 2024-12-19 15:13:48

Examples of complexity pattern的相关文章

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It

Java Logging: Handlers

Table of Contents Handlers and Formatters Built-in Handlers ConsoleHandler FileHandler File Name Pattern StreamHandler SocketHandler MemoryHandler A Handler is a component that takes care of the actual logging to the outside world. You can add one or

MapReduce Design Patterns

1. Design Patterns and MapReduce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Design PatternsMapReduce HistoryMapReduce and Hadoop RefresherHadoop Example: Word CountPig and Hive 2. Summarization Patter

Model-View-ViewModel (MVVM) Explained 转摘自:http://www.wintellect.com/blogs/jlikness/model-view-viewmodel-mvvm-explained

The purpose of this post is to provide an introduction to the Model-View-ViewModel (MVVM) pattern. While I've participated in lots of discussions online about MVVM, it occurred to me that beginners who are learning the pattern have very little to go

linq 动态组合条件

http://www.albahari.com/nutshell/predicatebuilder.aspx Dynamically Composing Expression Predicates Suppose you want to write a LINQ to SQL or Entity Framework query that implements a keyword-style search. In other words, a query that returns rows who

LeetCode Word Pattern II

原题链接在这里:https://leetcode.com/problems/word-pattern-ii/ 题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in

External Configuration Store Pattern 外部配置存储模式

Move configuration information out of the application deployment package to a centralized location. This pattern can provide opportunities for easier management and control of configuration data, and for sharing configuration data across applications

Learning JavaScript Design Patterns The Module Pattern

The Module Pattern Modules Modules are an integral piece of any robust application's architecture and typically help in keeping the units of code for a project both cleanly separated and organized. In JavaScript, there are several options for impleme

IoC Containers and the DI pattern

In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they r