20. The complexity of the algorithm

Source: Internet
Author: User


The complexity of the algorithm is divided into time complexity and space Complexity. Its function: time complexity is the length of time to measure the algorithm execution, and spatial complexity is the size of the storage space required by the measure Algorithm.

Complexity of Time

Time complexity describes the relationship of an algorithm to data size and execution Time.

For a simple example, to add from 0 to n, we would write:
int sum = 0;
for (int i = 0; i<=n; ++i)
{
Sum + = i;
}
Sum up n times add, then say this time complexity is O (n). Of course the precise concept of O (n) is that it is the highest order of n, for example, a calculation of a total of 3n + 2 times, Then this time complexity is O (n), because the highest in 3n + 2 is N.

If the code is written like This:
int sum = 0;
for (int i = 0; i<=n; ++i)
{
for (int j = 0; J <=n; ++j)
{
Sum + = (i + j);
}
}

It is clear that altogether n^2 addition, then said this time complexity is O (n^2), and similar above, if an algorithm calculates 3*n^2 + n + 1 times, Its time complexity is still o (n^2), because 3*n^2 + N + 1 The highest secondary is n^2

The so-called O (1) is the number of calculations is a constant, we also in the above from 0 to n example, if we use arithmetic progression formula, then, the code can write:
int sum = n * (n + 1)/2
No matter how big N (of Course not overflow), through the above formula only one calculation, also said the number of calculations is constant, the time complexity of this situation can be said to be O (1). For example, If a calculation, no matter how other conditions change, can only calculate 5 times to produce results, then the time complexity of this situation is also o (1).

Pick

Complexity of space

is the program to run the additional consumption of storage space, the general recursive algorithm will have o (n) space complexity, simply said recursive imputation is usually called the same method repeatedly, recursive n times, you need n space.

In general, space complexity is not considered, space complexity is not the space occupied by all the data, but the use of the size of the auxiliary space, such as the operation of two matrices, in the middle set up an intermediate matrix to save some data, these spaces are called spatial Complexity. Spatial complexity of the operation is very cumbersome, the general simple algorithm space complexity is O (1), more complex will tell the complexity of space, remember it is good.

Pick

20. The complexity of the algorithm

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.