Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the time complexity of the algorithm

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report