1、时间复杂度
时间复杂度也就是所说的算法执行所需要耗费的时间,时间越短,算法越好。
第一,算法代码执行的时间往往和算法代码中语句执行的数量有关。
第二,算法的时间复杂度还与问题的规模有关。
Big O notation
O(1)常数复杂度
eg: int n = 1000
system.out.println("");
O(log n)对数复杂度
eg: for(int i = 1; i <= n; i=i*2){
system.out.println("Hey - I'm looking at : +i");
}
O(N)线性时间复杂度
eg: for(int i = 1; i <= n; i++){
system.out.println("Hey - I'm looking at : +i");
}
O(N^2)平方 O(N^3)
eg: for(int i = 1; i <= n; i++){
for(int ij= 1; ij <= n; j++){
system.out.println("Hey - I'm looking at : +i+ and +j");
}
}
O(2^n)指数
eg: for(int i = 1; i <= math.power(2,n); i++){}
O(n!)阶乘
eg: eg: for(int i = 1; i <= factorial(n); i++){}
只看最高复杂度
例如:
Fibonacci array: 1 1 2 3 5 8 13 21
F(n) = F(n-1)+F(n-2)
常见算法复杂度计算公式:
2、空间复杂度
空间复杂度指的是算法程序在计算机中执行所需要耗费的内存空间。主要分为如下方面:
a. 程序保存所需要的存储空间,也就是程序的大小;
b. 程序在执行过程中做需要消耗的存储空间资源,例如,程序在执行过程的中间变量等。