Analysis of algorithm complexity

Source: Internet
Author: User

Push to the Big O-step method:

1. Replace all the addition constants in the run time with constant 1.

2. In the modified run Count function, only the highest order is preserved.

3. If the highest order exists and is not 1, the constant multiplied by the item is removed.

The result is the Big O-order.

Constant order

If there are no loops and recursive statements, just a simple sequential structure, then the algorithm complexity is O (1).

For branch-branch structures, the number of executions is constant regardless of whether they are true or false, and does not change with the change of N, so the simple branching structure, the time complexity is also O (1).

Linear order

The loop structure of the linear order is more complex, such as:

int sum = 0; 1

for (int i = 1; i <=; i++)//n+1

{

sum+= i; N

}

printf ("%d", sum); 1

The algorithm performs a total of 2n+3 times,

So the algorithm complexity is O (n)

Logarithmic order

int count = 1;

while (Count < N)

{

Count = Count * 2

}

The result is 2^x >= n

Then x = log2n

So the complexity of O (LOGN)

Square Order

int i,j;

for (i = 0; i < n; i++)

{

for (j = 0; J < N; j + +)

{

....

}

}

The time complexity is O (n^2);

If the number of loops in the inner and outer layers is different, then the complexity is O (MXN)

Common time complexity

Number of executions function Order Informal terminology
12 O (1) Constant order
2n+3 O (N) Linear order
3n^2+2n+1 O (n^2) Square Order
5log2n+20 O (LOGN) Logarithmic order
2n+3nlog2n+19 O (NLOGN) Nlogn Order
6n^3+2n^2+3n+4 O (n^3) Cubic order
2^n O (2^n) Exponential order

Common time complexity is consumed from small to large sort

O (1) <o (LOGN) <o (n) <o (NLOGN) <o (n^2) <o (n^3) <o (2^n) <o (n!) <o (N^n)

For algorithms above the exponential level, the basic is invalid and not computable.

Worst Run time

The worst-run time is the maximum time that the algorithm will run under the conditions, generally speaking, the complexity of the algorithm refers to the worst-case complexity.

Algorithmic control complexity

The spatial complexity of the algorithm is realized by calculating the storage space required by the algorithm, and the computational formula of the spatial complexity of the algorithm is as follows: S (n) = O (f (n)), where n is the scale of the problem, and F (n) is the function of the statement about the storage control occupied by N.

Instructions, constants, variables and input data are not related to the algorithm itself, so only the additional space needed to calculate the algorithm can be used.

Analysis of algorithm complexity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.