How to understand the time complexity of the algorithm
This article mainly explains "how to understand the time complexity of the algorithm". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to understand the time complexity of the algorithm.
We can express it with the following expression:
There are usually the following main expressions to describe time complexity:
O (1): constant time
O (n): linear time
O (log n): logarithmic time
O (n ^ 2): quadratic time
O (2 ^ n): exponential time
O (n!): factorial time
Each time complexity is different, let's learn more about these time complexities.
Large O complexity
O (1)
O (1) denotes constant time complexity. When an input of size n is given, no matter what the value of n is, the final execution time of the algorithm is constant. For example:
Int func (int n) {nails; return natives 2;}
In the above program, no matter how the value of input n changes, the program execution time is always constant. Let's simplify this: if the execution time of each line of statements in the function is 1, the mathematical expression of the execution time:
No matter how big n is, the final execution time is a fixed value of 2. Although the elapsed time is 2, here we also express it as O (1), where 1 represents a constant.
O (n)
O (n) represents linear time complexity, and the execution time of the algorithm varies linearly with the size of the input n.
Int func (int n) {int sum = 0; for (int iTuno; I)