php algorithm complexity and time complexity and space complexity

Complexity of the algorithm is divided into time complexity and space complexity.
Its effect:
the time complexity of the algorithm means performs computational effort required;
the spatial complexity refers to the execution memory space required for the algorithm.
(The number of computer resources required reflected the complexity of the algorithm is running the algorithm, the most important computer resources are time and space (ie register) resources, complexity into time and space complexity).

simply put,

Time complexity refers to the number of times the statement is executed
spatial complexity refers to the storage space occupied by the algorithm

time complexity

执行算法所需的计算工作量。一般来说,计算机算法是问题规模n的函数f(n),算法的时间复杂度也因此记做T(n)=O(f(n));
常见时间复杂度有:常数阶、线性阶、平方阶、立方阶、对数阶、nlog2n阶、指数阶
效率:O(1) > O(log2n)> o(n)> o(nlog2n) > o(n^2) > o(n^3) > o(2^n) > o(n!) > o(n^n) 

Other concepts

最坏情况:最坏情况时的运行时间,一种保证。如果没有特别说明,说的时间复杂度即为最坏情况的时间复杂度

Time complexity of calculation

Example: Calculation 1 + 2 + 3 + .... + n, and

$sum=0

for($i=1;$i<=$n;$i++){
$sum+=$i
}

N times the cycle can be seen, the time complexity is O (n), is the time complexity of the program calculates the number of
additional instructions

1. All additive constant to replace all the time by a constant 1

比如上面的例子中,不管n为多少,计算次数都是3,那么时间复杂度为O(1),而不是O(3) 

2. In the Run function in the revised number, retaining only the highest order term

比如运算的次数为n^2+1,那么为时间复杂度为o(n^2) 

3. If the highest-order exists and is not 1, then multiplied by a constant and the removed item

2n^2+3n+1 ->n^2

Why remove these values, see the figure

When the calculated amount larger as the number of the original time, and the difference between n 1 is not too large, and n 2 the curve becomes larger, so this is 2N 2 + 1 with 3N + -> n 2 is estimated to be the last n 2 reasons, since 3n + 1 is calculated as the number becomes larger, substantially negligible, the other similar
constant order O (1)

function test($n){
echo $n;
echo $n;
echo $n;
}

No matter how much $ n, run only three times, then the time complexity is O (3), taken as O (1)
linear order O (n)

for($i=1;$i<=$n;$i++){
$sum+=$i
}

Level (Li) Party order: O (n- 2) / O (n- . 3)

$sum=0;
for($i=1;$i<=$n;$i++){
for($j=1;$j<$n;$j++){
$sum+=$j
}
}

两次循环,里面循环执行了n次,外层循环也执行了n次,所以时间复杂度为O(n^2),立方阶一样

Special order of the square: O (n- 2 / n-2 + / 2) -> O (n- 2)

for(){
for(){
..... ----------->n^2
}
}
+
for(){
------------> n
}

                              +

echo $a+$b --------------> 1

所以整体上计算次数为n^2+n+1,我们算时间复杂度为O(n^2) 

On the order of: O (log2n)

If a power of x is equal to N (a> 0, and a is not equal to 1), then the number x is called as a substrate in a number of N (logarithm), denoted by x = logaN. Where, a known base number of the number, N is called the real numbers. [1]

while($n>=1){
$n=$n/2;
}

n number of executions
. 1. 1
2 2
. 3 2

law:

The first sequence a second sequence of three fourth fifth
20---------> 10----------> 5--------> 2.5 --- ---->. 1
NN / n-2/2 / n-2/2/2 / n-2/2/2 / ...

All Law: n / (2 ^ m) = 1; we need calculating m, is converted into n = 2 ^ m, stars m = log2n, so the time complexity is O (logn) or O (log2n)
spatial complexity

Algorithms need to consume memory space. Namely S (n) = O (f (n)); comprising program code space occupied, the space occupied by the data input and auxiliary variables space occupied by these three aspects. Calculation and Expression of the time complexity similar manner, generally with asymptotic complexity of the expressed

关于O(1)的问题, O(1)是说数据规模和临时变量数目无关,并不是说仅仅定义一个临时变量。举例:无论数据规模多大,我都定义100个变量,这就叫做数据规模和临时变量数目无关。就是说空间复杂度是O(1)。

Space complexity calculation

举例:冒泡排序的元素交换,空间复杂度O(1)
冒泡排序就是两两交换,中间设置临时变量存储交换的值,不管要交换的数据多大,临时变量始终为固定数量

Bubble sort: the $ arr = [1,3,2,4,6,5] to sort it out

Principle: two adjacent numbers are compared, if the reverse order exchanged, otherwise no exchange
132465

1 and 3 is first compared Fixed
132465

2 and 3 again compared exchange
123465

3 and 4 again compared Fixed
123465

4 and 6 again compared Fixed
123465

5 and 6 again compared exchange
123456

for($i=0;$i<=$n;$i++){
for($j=0;$j<=$n;$j++){

if($arr[$j]>$arr[$j+1]){
   $tmp=$arr[$j]; 
  $arr[$j]=$arr[$j+1];
  $arr[$j+1]=$tmp;
}
}

}

Therefore, the time complexity is O (n ^ 2), the spatial complexity is O (1)
common sorting algorithm

冒泡排序、直接插入排序、希尔排序、选择排序、快速排序、对排序、归并排序

Common Search Algorithm

二分查找、顺序查找

Expansion: gradual growth function

https://blog.csdn.net/raylee2...

Guess you like

Origin www.cnblogs.com/djwhome/p/12554474.html