Computation of algorithm time complexity and space complexity

Source: Internet
Author: User

Algorithm, that is, the method of solving the problem. The same problem, the use of different algorithms, although the results are the same, but the time spent and resources are different.


For example, to twist a nut, the use of wrenches or pliers are different, although the use of pliers can also screw nuts, but there is no wrench good.

"All roads lead to Rome", there are many algorithms to solve the problem, it is necessary to determine which algorithm is "better".

Algorithm vs Program

Many people mistakenly think that the program is an algorithm, but it is not: the algorithm is to solve a problem of ideas, ideas, and the program is in the heart of the premise of the algorithm can be written out of the code to run.

For example, to solve the problem of outputting the value of a data element in a one-dimensional array, the first thought is to use the loop structure (for or while) to start writing programs based on the algorithm.

Therefore, the algorithm is equivalent to the embryonic form of the program. When solving the problem, first of all, the problem-solving algorithm, around the algorithm to write program code.

is there an algorithm that will solve the problem?

For a problem, come up with a solution to the algorithm, not necessarily can solve this problem.

For example, the screw nut, the wrench is better than the pliers (the process of selecting the algorithm), but in the process of screwing (the process of programming) found that the nut rusty twist, then need to find another way.

To avoid this situation, consider the problem fully and comprehensively, taking into account all possible situations, and choosing the algorithm carefully (need to accumulate experience in practice).

standard for "good" algorithms

For a problem algorithm, which is called an algorithm, first it must be able to solve this problem (known as accuracy). Second, programs written through this algorithm require that they not crash in any situation (known as robustness).

If accuracy and robustness are met, then the most important thing to consider: How efficient is the program written by the algorithm.

Operational efficiency is reflected in two ways:

    • The run time of the algorithm. (called "Time Complexity")
    • The amount of memory space required to run the algorithm. (called "Spatial complexity")


The standard of good algorithm is: In accordance with the requirements of the algorithm itself, the use of the program to write programs run short time, the running process occupies less memory space, you can call this algorithm is "good algorithm."

surveys show that people have a high demand for software or apps, for example, the endurance limit for Web pages is 6 seconds or less, and if you design a Web page that is open for more than 6 seconds, most will switch off without hesitation at 4 seconds or even 3 seconds to browse other pages. In this context, a good "algorithm" is more focused on time complexity, and space complexity as long as within a reasonable range can be. computation of time complexity

Calculate the time complexity of an algorithm, it is impossible to all the algorithms are written out of the actual program to let the computer run, this will do a lot of useless, inefficient. The actual method used is to estimate the time complexity of the algorithm.

In learning the C language, the program consists of three structures: sequential structure, branching structure and cyclic structure. Each piece of code in the sequential structure and the branching structure is run only once, and the code in the looping structure depends on the number of times the loop is run.

Because it is the time complexity of estimating algorithm, the cyclic structure has more influence on the execution time of the algorithm than the other. Therefore, the time complexity of the algorithm, the main view of the algorithm used in the loop structure of the code loop number of times (called "Frequency"). The lower the number of times, the less time complexity of the algorithm.

For example:
a) ++x; s=0;
b) for (int i=1; i<=n; i++) {++x; s+=x;}
c) for (int i=1, i<=n; i++) {for (int j=1; i<=n; J + +) {++x; s+=x;}}

In this example above, the a code runs 1 times, B code runs n times, C code runs n*n times.

representation of time complexity

The time complexity of the algorithm is expressed in the following ways:

O (Frequency)

This representation is called 大“O”记法 .

Note that it is uppercase letters O , not numbers 0 .

For the above example, the time complexity of A is O(1) , B's time complexity is O(n) , and C's time complexity is O(n2) .

If a, B, C constitute a program, then the time complexity of the algorithm is O(n2+n+1) . But it is wrong to say so, and it needs to n2+n+1 be simplified.

The simplified process is summarized in 3 steps:

    • Removes all addition constants from the run time. (e.g. n2+n+1, directly into N2+n)
    • Keep only the highest item. (N2+n becomes N2)
    • If the highest item exists but the coefficient is not 1, remove the factor. (N2 coefficient is 1)


So, the time complexity of the code that eventually merges a, B, and C is O(n2) .

The sorting of common time complexity

Several common algorithms for time complexity comparisons (small to Large) are listed:

O(1)常数阶 < O(logn)对数阶 < O(n)线性阶 < O(n2)平方阶 < O(n3)(立方阶) < O(2n) (指数阶) take time to change space, use space to change time

The time complexity and spatial complexity of the algorithm can be converted to each other.

Google's browser runs faster than other browsers. Because it takes up more memory space and takes space for time.

Algorithm, for example, to determine whether a year is a leap years, if you want to exchange time for space, the idea is: when given a year, to determine whether the year can be 4 or 400 divisible, if possible, is leap years.

If you want to change time with space, the idea of judging leap years is: To determine all the year first, stored in the array (the year and the subscript corresponding), if it is a leap year, the array value is 1, otherwise 0; when it is necessary to determine whether a leap years, the corresponding array value is 1 or 0, without calculation can immediately know.

Computation of algorithm 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.