How to judge the level of time complexity

First, let's talk about what time complexity is:

(From Baidu Encyclopedia) In computer science , time complexity is also called time complexity . The time complexity of an algorithm is a function that qualitatively describes the running time of the algorithm. This is a function of the length of the string representing the input value of the algorithm . Time complexity is often expressed in big O notation , excluding the low-order term and first coefficient of this function. When using this method, the time complexity can be said to be asymptotic , that is, when the input value approaches infinity.

So how to judge whether the time complexity is large or small?

The order of time complexity is O(1) <O(logn) <O(n) <O(nlogn) <O(n^2) <O(2^n) <O(n!) <O(n^ n).

So how do you know which level of time complexity is?

Let me talk about O(1) first, which means that the code did not run all the code, and did not search in a loop, but directly found what it needed, for example, searching for the fifth data in a row of data and giving the storage address directly. It needs to be compared one by one, and the time complexity of directly going to the specified location to get the data is O(1).

The complexity of O(n) is that there is a for loop that loops through the contents of a table and returns the result after finding the required contents. As shown below:

O(n^2) is a nested for loop. In the for loop, a for loop is nested. The inner loop loops once, the outer loop loops once, and then the inner loop loops once. Is it equivalent to running? n*n times, as shown below :

 

The logarithmic order O(log n) is usually recursive. In general, the time complexity of the recursive algorithm is generally O(log n).

It is stated that these algorithms are an estimate of time complexity and are not definite, but this is indeed a measure of code quality.

Generally, the common time complexity needs only these few. The current bloggers who have other complexity corresponding to the code don't know. If they learn it one day, they will update the blog in this chapter again.

Guess you like

Origin blog.csdn.net/weixin_37081112/article/details/108711115