Day5 time complexity, day5

Source: Internet
Author: User

Day5 time complexity, day5

Time Complexity

(1)Time Frequency The time it takes to execute an algorithm cannot be calculated theoretically. You must run the test on the computer before you can understand it. However, we cannot and do not need to perform Machine tests on every algorithm.It takes a lot of time for an algorithm.. 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 complexityIn the Time Frequency just mentioned, n is called the scale of the problem. When n is constantly changing, 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,T (n)/f (n)F (n) is the same order of magnitude function of T (n. NoteT (n) = O (f (n )),NameO (f (n ))It is the progressive time complexity of the algorithm.

Exponential time

The computing time required for solving a problem.M(N), Exponential growth based on the size of input data (that is, the number of input data grows linearly, and the time spent will grow exponentially)

For (I = 1; I <= n; I ++)

X ++:

For (I = 1; I <= n; I ++)

For (j = 1; j <= n; j ++)

X ++;

The time complexity of the first for loop is round (n), and the time complexity of the second for loop is round (N2), the time complexity of the entire algorithm is round (n +N2) = equals (N2 ).

Constant time

If the upper bound of an algorithm is irrelevant to the input size, it hasConstant time,O (1)Time. One example is to access a single element in the array, because only one instruction is required to access it. However, finding the smallest element in the unordered array is not because it needs to traverse all elements to find the minimum value. This is a linear time operation, or time. However, if you know the number of elements in advance and assume that the number remains unchanged, this operation can also be called a constant time.

Logarithm time

If the algorithmT(N) = O (logN), It indicates that it has a logarithm time.

 

Common algorithms with logarithm time include binary tree related operations and binary search.

 

The algorithm for logarithm time is very effective, because the extra computing time required for each input is reduced.

 

Recursively cutting the string half and outputting it is a simple example of this class function. It requires O (log n) time because we cut the string half before each output. This means that if we want to increase the number of output times, we need to double the string length.

Linear Time

 

If the time complexity of an algorithm is O (N), The algorithm has linear time, or O (N) Time. In short, this means that for a large enough input, the increase in the running time is linearly related to the input. For example, the time required for a program to calculate the sum of all elements in the list is proportional to the length of the list.

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.