Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Time complexity and efficiency of algorithm (2)

Shulou Source: shulou.com Published: 2022-06-03 05:14:44 10月05日 Update

Today, let's take a look at the complexity and efficiency of the algorithm. when judging the efficiency of an algorithm, the constant terms and other secondary terms in the number of operations can often be ignored, and we only need to pay attention to the highest-order terms to draw a conclusion. So how do we use symbols to qualitatively judge the efficiency of the algorithm? The complexity of the algorithm is divided into two parts: 1, time complexity, that is, the qualitative description of the time demand after the algorithm is running; 2, space complexity, that is, the qualitative description of the space demand after the algorithm is running.

The data structure focuses on the efficiency of the algorithm, so we will focus on the time complexity of the algorithm later, but the method used can be used to judge the space complexity! We often use the large O representation to analyze the time complexity of the algorithm. Next to explain this method, the efficiency of the algorithm depends heavily on the number of operations (Operation); the estimation of the number of operations can be used as an estimation of time complexity; in the judgment, we first pay attention to the highest number of operations. As follows:

Let's analyze the common time complexity:

1. Linear order time complexity: O (n). As follows:

2. Logarithmic time complexity: O (logn). As follows

3. Square order time complexity: O (n ²). As follows:

Let's take a look at the common time complexity, as shown in the following figure

A common comparison of time complexity is as follows

Let's analyze the complexity of the following function program through an example.

Int find (int a [], int n, int v) {int ret =-1; for (int iTuno; I)

Tags: Complexity complexity algorithm time space situation efficiency quantity demand analysis common program problem two function array method best representation demand Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology Xiaomi macOS Redmi