Time complexity and spatial complexity of Java

Source: Internet
Author: User

The same problem can be solved by different algorithms , and the quality of an algorithm will affect the efficiency of the algorithm and even the program . The purpose of the algorithm analysis is to select the suitable algorithm and the improved algorithm.

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.

1. Complexity of Time

1.1 Time Frequency

The number of times a statement is executed in an algorithm is called a statement frequency or time frequency. Remember as T (N)

1.2 Time complexity

Under normal circumstances, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when n approaches infinity, the limit value of T (n)/f (n) is not equal to zero constant, then f (n) is the same order of magnitude function of t As T (n) =o (f (n)), called O (f (n)) is the progressive time complexity of the algorithm, which is referred to as the complexity of time.

In various algorithms, if the algorithm is a constant number of execution times, the time complexity is O (1), in addition, the time frequency is not the same, the time complexity may be the same, such as T (n) =n2+3n+4 and T (n) =4n2+2n+1 their frequency is different, but the time complexity of the same, all O ( N2).

In order of magnitude increment, the common time complexity is: Constant order O (1), Logarithmic order O (log2n), linear order O (n), linear logarithmic order O (nlog2n), square order O (n2), Cubic O (n3),..., K-order O (NK), exponent-order (2n).

With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less.

2. Complexity of Space

The spatial complexity of an algorithm (space complexity) S (n) is defined as the storage space consumed by the algorithm, and it is also a function of the problem size n. Asymptotic spatial complexity is also often referred to as spatial complexity.

The storage space occupied by an algorithm in the 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 occupied by the algorithm in the running process three aspects. The storage space occupied by the input and output data of the algorithm is determined by the problem to be solved, which is passed by the calling function by the parameter table, and it does not change with the algorithm. Storage algorithm itself occupies the storage space and the length of the algorithm written in proportion, to compress the storage space, you must write a shorter algorithm. The storage space temporarily occupied by the algorithm varies with the algorithm, and some algorithms only need to occupy a small amount of temporary work units, and do not change with the size of the problem.

The spatial complexity of an algorithm only considers the size of the storage space allocated for the local variables during the run, including the storage space allocated for the parametric in the parameter table and the storage space allocated for the local variables defined in the function body two parts. If an algorithm is a recursive algorithm, its spatial complexity is the size of the stack space used by recursion, which is equal to the size of the temporary storage space allocated for a call multiplied by the number of times called (that is, the number of recursive calls plus 1, the 1 table does not start a non-recursive call). The spatial complexity of the algorithm is usually given in order of magnitude. If the spatial complexity of an algorithm is a constant, that is, it can be represented as O (1) when it is not changed by the size of N of the processed data, and when the spatial complexity of an algorithm is proportional to the logarithm of the base N of 2, it can be represented as 0 (10g2n), and when an algorithm's empty I-division complexity is linearly proportional to N, can be represented as 0 (n). If the parameter is an array, it is only necessary to allocate a space for it to store an address pointer transmitted by the argument, that is, a machine word space, and if the formal parameter is a reference, it is only necessary to allocate a space for it to store the address of the corresponding argument variable. To automatically reference the argument variable by the system.

For an algorithm, its time complexity and spatial complexity are often influenced by each other. When the pursuit of a better time complexity, the performance of spatial complexity may be poor, that may lead to more storage space; Conversely, when =i self-seeking a better spatial complexity, the performance of time complexity may become worse, which may lead to a longer running time. In addition, all the performance of the algorithm has more or less mutual influence. Therefore, when designing an algorithm (especially large algorithm), we should consider the performance of the algorithm, the frequency of use of the algorithm, the size of the data amount processed by the algorithm, the characteristics of the algorithm description language, the machine system environment of the algorithm running, and so on, to design a better algorithm.

Time complexity and spatial complexity of Java

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.