PHP Algorithm Foundation----time complexity and space complexity

Source: Internet
Author: User

The complexity of the algorithm is divided into time complexity and space complexity.

Its role:
Time complexity refers to the computational effort required to perform the algorithm;
Spatial complexity refers to the amount of memory space required to execute the algorithm.
(The complexity of the algorithm is reflected in the amount of resources required to run the algorithm, the most important thing in computer resources is time and space (that is, register) resources, so complexity is divided into time and space complexity).

In short, time complexity refers to the number of execution times, the space complexity refers to the storage space occupied by the algorithm

Complexity of Time
Methods for calculating time complexity:

    1. Replace all addition constants in run time with constant 1
    2. Only the highest order is kept in the modified run Count function
    3. Coefficients for removing the highest order

In order of magnitude increments, common time complexity is:
A constant order O(1) , a logarithmic order, O(log2n) 线性阶O(n)
Linear logarithmic order O(nlog2n) , square order O(n^2) , cubic order O(n^3) ,...,
K O(n^k) -order, exponent-order O(2^n) .
With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less.

Give me a chestnut:

sum = n*(n+1)/2;        //时间复杂度O(1)

for(int i = 0; i < n; i++){    printf("%d ",i);} //时间复杂度O(n)

for(int i = 0; i < n; i++){    for(int j = 0; j < n; j++){ printf("%d ",i); }} //时间复杂度O(n^2)

for ( int i = 0; i < n; i++) {for (int j = I J < N; J + +) {printf ( "%d", I);}} Number of runs (1+n) *n/2//time complexity O (n^< Span class= "Hljs-number" >2)             
int i = 0, n = 100;while(i < n){    i = i * 2;}//设执行次数为x. 2^x = n 即x = log2n//时间复杂度O(log2n)

Worst time complexity and average time complexity
The worst-case time complexity is called the worst time complexity. In general, it is not particularly stated that the time complexity of the discussion is the worst-case time complexity.
The reason for this is that the worst-case time complexity is the upper bound of the algorithm's run time on any input instance, which guarantees that the algorithm will not run longer than any other.
The average time complexity is the expected run time of the algorithm when all possible input instances are present with equal probabilities. The probability of the occurrence of each case is pi, and the average time complexity is sum (PI*F (n))
 

Time complexity of common sorting algorithms

Worst time analysis average time complexity stability spatial complexity bubble sortO (N2)O (n2) stableO (1) Quick sort o (n2) o (n*log2n) unstable o (log2n) ~o (n) Select sort o (n2) o (n2) Stability o (1) binary tree sort  O (n2) o (n*log2n) unstable o (n) Insert sort O ( N2) o (n2) stable o (1) heap sort o (n*log2n) o (n*log2n) unstable O (1) Hill sort o o unstable O ( Span class= "Hljs-number" >1)             

Complexity of Space
Spatial complexity (space complexity) is a measure of how much storage space is temporarily occupied by an algorithm while it is running, and is recorded as S (n) =o (f (n)).

For an algorithm, spatial complexity and time complexity are often influenced by each other. When the pursuit of a better time complexity, the performance of the spatial complexity may be poor, that is, may lead to more storage space; Conversely, when the pursuit of a better spatial complexity, the performance of time complexity may be poor, which may lead to a longer run time.

Sometimes we can use space in exchange for time to achieve our goal.

PHP Algorithm Foundation----time complexity and space 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.