Big talk Data Structure algorithm time complexity

Source: Internet
Author: User

Buddha said: the debt owed will be paid back later. Ah, the university is not good at learning the data structure. Do it now. Debt repayment !!!

 

The time complexity is embodied in O (), which is called the olog method. Note: T (n) = O (f (n ))

Generally, as n increases, T (n) is the slowest growing algorithm.

O (1) is called constant order O (n) is called linear order O (n2) is called square order

 

Push down big O-level

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

2. In the modified number of running functions, only the highest-order items are retained.

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

The result is a large level of O.

 

For the Gaussian algorithm, the number of times the function runs is f (n) = 3. According to our derivation of the Large O-order method, the first step is to change the constant item 3 to 1. When the highest-order item is retained, it is found that it does not have the highest-order item at all, so the time complexity of this algorithm is O (1 ).

 

For the branch structure, whether it is true or false, the number of executions is constant and will not change with the increase of n, therefore, the time complexity of a pure branch structure (not included in the loop structure) is O (1 ).

 

Linear order:

Int I;

For (I = 0; I <n; I ++)

{

}

Because the code in the loop body needs to be executed N times, the time complexity of its loop is O (n ).

 

Logarithm level:

Int count = 1;

While (count <n)

{

Count = count * 2;

}

Since each count multiplied by 2, it is closer to n. That is, the number of 2 after multiplication is greater than n, the loop will exit. Obtain x = log2n from 2x = n. Therefore, the time complexity of this loop is O (logn)

 

 

The worst-case running time is a guarantee, that is, the running time will not break down. In applications, this is the most important requirement. Generally, unless specified, the running time we mentioned is the worst-case running time.

 

 

The complexity of the algorithm space is realized by the storage space required by the computing algorithm. The formula for calculating the complexity of the algorithm space 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 space occupied by n.

Common time complexity

O (1) <O (logn) <O (n) <O (nlogn) <O (n2)

 

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.