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

An example Analysis of time complexity in C language

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

Share

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

This article shows you the sample analysis of time complexity in C language, which is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Time complexity 1. What is time complexity?

Space efficiency, time efficiency (more concerned)

Time complexity: the number of operations performed in the algorithm, which is the time complexity of the algorithm. (not the specific time, but the number of execution)

two。 How is it calculated?

Time complexity

(1) it is an estimate, depending on which term in the expression has great influence, for example, in N*N+2N+10, N has the greatest influence on the whole formula, so its time complexity is N N, using the asymptotic representation of large O O (N N).

(2) remove the product of constant terms in the time expression, for example, if an accurate time expression is 2N+10, the estimated time complexity is O (N).

(3) for multiple unknowns, for example, if the time expression is Numerm, then O (M) or O (N) is assumed that M is almost large, and O (M) is assumed that M is much greater than N.

(4) replace all the determined constants with the constant 1, for example, the exact time expression is 100dint O (1).

(5) unknowns and constants, filter out constants.

(6) some other algorithms are divided into the best, the worst and the average. When the algorithm has these three situations, choose the worst time complexity, for example, suppose the string length is N, "sdsfrsgtr...", traverse the string, find the time complexity of S, the best O (1), the worst O (N), the average O (N).

a. In bubbling sort

The first trip is bubbling: n

The second trip is bubbling: NMUI 1

The third trip is bubbling: NMIT 2

The Nth Bubble: 1

It is an arithmetic sequence, and the exact degree is N* (Niss1) / 2.

Therefore, the time complexity of bubble sorting is O (Numn).

b. In binary search / half search:

Suppose there are two X times, there are 1'2'2 2'N, 2 ^ X = N, X = (log2) N

In the complexity calculation of the algorithm, I like to omit it as logN, because it is difficult to write the base number, but it is wrong to write it as lg N.

c. Calculate the time complexity in some factorial operations:

Long long Factorial (size_t N) {return N1, found, but the time complexity does not match.

Idea 2: add 0 to N, and the result is ret1, and then add up the numbers in the array. Ret2,ret1-ret2 is the number you are looking for.

Idea 3: XOR: the same is 0 and the difference is 1. XOR the number in the array with the number 0murn, and the last remaining number is the missing one.

Int missingNumber (int* nums, int numsSize) {int xonomo; / / perform XOR for with the number of the array first (int ionome0polii)

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