Data structure (C description) algorithm time and space complexity

Algorithm: An algorithm is a limited set of rules, a series of operations prescribed to solve a specific problem.
characteristic:

1.有限性:有限步骤内正常结束,不能形成无穷循环
2.确定性:算法中的每一个步骤必须有确定含义,无二义性
3.可行性:原则上能精确进行,操作可通过已经实现的基本运算执行有限次而完成
4.输入:有多个或0个输入
5.输出:至少有一个或多个输出

Design requirements

正确性
可读性
健壮性(鲁棒性)
高效率和低储存量

Algorithm time spent : The execution time of an algorithm refers to the sum of the execution time of all statements in the algorithm. The execution time of each statement is equal to the number of executions of the statement multiplied by the actual time required for one execution;
statement frequency : the statement is in The number of repeated executions in an algorithm, the time consumption of an algorithm is the sum of the frequency of all sentences in the algorithm;

Time complexity:
For algorithm analysis, the concern is that the total number of executions of sentences in the algorithm f(n) is a function of the problem size n, and then analyze the change of f(n) with n and determine the order of magnitude of T(n); here Use "O" to represent the order of magnitude, and the time complexity of the algorithm T(n)=O(f(n));
in the analysis of the algorithm, the time complexity and the progressive time complexity of the algorithm are often indistinguishable; the
worst case The next time is called the worst time complexity in terms of complexity .
Average time complexity refers to the expected running time of the algorithm when all possible input instances appear with equal probability;

Commonly used time complexity frequency count (arranged in increasing number):

O(1)常数阶
O(log2n)对数阶
O(n)线性阶
O(nlog2n)线性对数阶
O(n^2)平方阶
O(n^3)立方阶
O(n^k)K次方阶
O(2n)指数阶

Space complexity:
The space occupied by an algorithm refers to the sum of the auxiliary space actually occupied by the algorithm. The space complexity of the algorithm S(n) is defined as the order of magnitude of the storage space consumed by the algorithm, which is a function of the problem size n,
S(n)=O(f(n))
if the auxiliary space required for the algorithm execution Relative to the amount of input data, it is a constant, so this algorithm is called working in situ, and the auxiliary space is O(1);

Examples:
1) O(1) constant orderInsert picture description here
For branch structure, no matter how many sentences there are (not in the loop), its time complexity is O(1);

2) O(log2n) logarithmic order
First, let’s review the formula, I believe many people have forgotten
a^y=x→y=log(a)(x)[The formula means y=log is based on a x Logarithm, where a is the base and x is the true number. In addition, a is greater than 0, a is not equal to 1, and x is greater than 0]
Insert picture description here

The time complexity of binary search is O(log2n), if one comparison can achieve k equal divisions (that is, k-1 other equal lengths are excluded), the time complexity is O(logkn), and the order of complexity is the same.

3) O(n) linear order is
Insert picture description here
relatively simple, it is clear that the loop body has been executed n times;
4) O(n^2) square order
Insert picture description here

For the step loop statement, only the loop number of the statement in the loop body is considered. Obviously the frequency of x++ is n^2; the
cubic order and the multi-order are almost the same, but the higher order has no practical meaning, we just Not considered

5) O(2n) exponential order The
Fibonacci sequence is a very obvious example
Insert picture description here
. Those who don’t understand don’t need to be discouraged. Next, go directly to the portal, and the Fibonacci sequence formula derivation process: https: //wenku.baidu.com/view/54aa9443a8956bec0975e350.html

The above are all basic examples. There are also some complicated questions that are just deformed, which is not difficult, but some require mathematics. Just be careful.

Guess you like

Origin blog.csdn.net/weixin_47160672/article/details/108674504