Li Haha's data structure and algorithm notes [part 2: time complexity and space complexity]

Li Haha's data structure and algorithm notes [part 2: time complexity and space complexity]

Algorithmic efficiency measurement method

1. Post-statistical methods
Disadvantages: test procedures need to be numbered in advance, and different test environments vary greatly.
2. Pre-analysis and estimation method
definition : Before the computer program is written, the algorithm is estimated according to the statistical method.

Function progressive growth

There is a positive integer N. If n>N, f(n) is always greater than g(n), then the progressive growth of f is faster than g.

  • The addition constant can be omitted, such as 3n+2≈3n
  • The constant multiplied by the highest order term is not important and can be omitted

**Conclusion:** To judge the efficiency of the algorithm, the constants and other minor items in the function can often be ignored, and more attention should be paid to the order of the main item (the highest degree).

Algorithm time complexity

Refers to the running time requirements of the program.
Big O notation:

  1. Replace all addition constants in running practice with a constant 1
  2. In the modified number of runs function, only the highest order items are retained
  3. If the highest order term exists and is not 1, remove the constant multiplied by this image
  4. The final result is a big O order

There are several typical orders: constant order, linear order, square order, cubic order, logarithmic order, exponential order,
order comparison:
1 <logn <n <nlogn <n square <n cube <2^n <n! <n times square

Worst case and average case

A program, we will make the worst case of its operation, to measure its performance.
The average situation is also a measure, but the former is more commonly used.

The space complexity of the algorithm

Refers to the amount of space required by the algorithm.
Space can be exchanged for time when programming.
For example, querying for leap years can be realized by calculation, or by creating an array and adopting a query method.
Which one is better or worse depends on the actual situation.

Guess you like

Origin blog.csdn.net/qq_41883714/article/details/109498844