Time and space complexity of the algorithm

Source: Internet
Author: User
Time and space complexity of the algorithm

Time and space complexity of common algorithms

Sorting Method

Worst Time Analysis Average time complexity Stability Space complexity
Bubble Sorting O (n2) O (n2) Stability O (1)
Quick sorting O (n2) O (N * log2n) Unstable O (log2n )~ O (N)
Select sort O (n2) O (n2) Stability O (1)
Binary Tree sorting O (n2) O (N * log2n) Different O (N)

Insert sort

O (n2) O (n2) Stability O (1)
Heap sorting O (N * log2n) O (N * log2n) Unstable O (1)
Hill sorting O O Unstable O (1)

1. Time Complexity
(1) Time Frequency the time required for executing an algorithm cannot be calculated theoretically. You must run the test on the computer before you can understand it. However, it is impossible and unnecessary for us to test each algorithm on the machine. We only need to know which algorithm takes more time and which algorithm takes less time. In addition, the time spent by an algorithm is proportional to the number of statements executed in the algorithm. In an algorithm, when the number of statements executed is large, it takes more time. The number of statement executions in an algorithm is called the statement frequency or time frequency. As T (n ).
(2) time complexity in the Time Frequency just mentioned, n is called the scale of the problem. When n is constantly changing, the time frequency T (n) will also change. But sometimes we want to know what the rule is when it changes. Therefore, we introduce the concept of time complexity. In general, the number of repeated executions of the basic operation in an algorithm is a function of the problem scale N. It is represented by T (N). If an auxiliary function f (n) exists ), so that when n approaches infinity, the limit value of T (N)/F (n) is a constant not equal to zero, then f (n) is T (N). It is recorded as T (n) = O (f (n), and O (f (N) is the progressive time complexity of the algorithm.
In different algorithms, if the number of statement executions in the algorithm is a constant, the time complexity is O (1). In addition, the time complexity may be the same when the time frequency is different, for example, the frequencies of T (n) = n2 + 3N + 4 and T (n) = 4n2 + 2n + 1 are different, but the time complexity is the same, they are all O (n2 ). In ascending order of magnitude, common time Complexities include: constant order O (1), logarithm order o (log2n), linear order O (N), linear logarithm order o (nlog2n ), square order O (n2), cubic order o (N3 ),..., K to the power of O (NK), exponential order o (2n ). As the problem scale N increases, the time complexity increases and the algorithm execution efficiency decreases. 2. The space complexity is similar to the time complexity. The space complexity refers to the measurement of the storage space required for an algorithm to be executed in a computer. Note: S (n) = O (f (N) We generally discuss the scale of auxiliary storage units except for normal memory usage. The discussion method is similar to the time complexity.
(3) Evaluation of progressive time complexity the time performance of an algorithm mainly evaluates the time performance of an algorithm by the magnitude of the algorithm's time complexity (that is, the approximate time complexity of the algorithm.

2. space complexity)

Similar to the discussion of time complexity, the space complexity S (n) of an algorithm is defined as the storage space consumed by the algorithm. It is also a function of the problem scale N. The Approximate Spatial complexity is often referred to as spatial complexity. Space complexity is a measure of the size of storage space temporarily occupied by an algorithm during operation. The storage space occupied by an algorithm in computer memory, including the storage space occupied by the storage algorithm itself, the storage space occupied by the input and output data of the algorithm and the storage space temporarily occupied by the algorithm during running. The storage space occupied by the input and output data of an algorithm is determined by the problem to be solved. It is transmitted by calling a function through a parameter table, which does not change with the algorithm. The storage space occupied by the storage algorithm itself is proportional to the length of the algorithm writing. to compress the storage space, you must compile a short algorithm. The storage space temporarily occupied by an algorithm varies with the algorithm. Some algorithms only occupy a small amount of temporary work units and do not change with the size of the problem, we call this algorithm "in-place \" and save storage, as is described in this section; the number of temporary work orders that some algorithms need to occupy is related to the scale of Solving the Problem n. It increases with N. When n is large, it will occupy a large number of storage units, for example, the quick sort and merge sort algorithms described in Chapter 9 belong to this situation. For example, if the spatial complexity of an algorithm is a constant that does not change with the size of N, it can be expressed as O (1 ); when the spatial complexity of an algorithm is proportional to the logarithm of N at the bottom of 2, it can be expressed as 0 (10g2n ); when the complexity of an algorithm's null I division is linearly proportional to N, it can be expressed as 0 (n ). if the parameter is an array, you only need to allocate a space for storing an address pointer transmitted by the real parameter, that is, a machine word length space. If the parameter is a reference method, you only need to allocate an address space for it to store the address corresponding to the real variable, so that the system can automatically reference the real variable.

3. Relationship between time complexity and space complexity

When a computer completes a task, there are two indicators: time and all memory (that is, space ). The two are negatively correlated. That is to say, when you design a specific program, you can choose to use more memory, which can improve the program running speed, that is, reduce the program running time. On the other hand, you can also choose to use a small amount of memory, which can save memory, but at the same time the program running speed will slow down, that is, the program running will take more time. In short, there are only two strategies in the algorithm: Time for space or space for time.

Directly answering the question is that the time complexity of algorithms with high spatial complexity is low, and vice versa.

(7) which of the following statements is correct ________.

A) if an algorithm has a large space complexity, its time complexity must also be high.

B) if an algorithm has a large space complexity, its time complexity must be small.

C) if an algorithm has a high time complexity, its space complexity must be small.

D) None of the above three answers: Select D for parsing: if there is a "yes", it will be wrong. In some special cases, there are special cases with high time-space complexity or low time-space complexity at the same time. However, when a given bucket is generally given a memory of KB but not limited to a time, there is a negative correlation between the time space. For programs that do not limit time or space, the time complexity and space complexity of the algorithm can be both large and small. For example, if T (n) = O (N) and S (n) = O (1), for example, for (I = 0; I <n; I ++ ), if the cycle body is a variable that is not related to the problem scale, its S (n) = O (1), and T (n) = O (N) it changes with N. At this time, it can be said that the time complexity is small and the space complexity is small.

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.