Time Complexity and Space Complexity
1. Time complexity
1. What is time complexity?
算法的执行效率
算法的执行时间与算法的输入值之间的关系
2. What is big
O
notation?
O(N)
, whereN
refers to例1
innum
,N = num
【Example 1:】
def test(num):
total = 0 # 执行时间为:a;
for i in range(num):
total += i # 执行时间为:b;num次为:num * b;
return total # 执行时间为:c;
# 所以test类的时间复杂度 = a + num * b + c
So num很大
at that time , then test
the time complexity of the class is mainly based on num * b
. So this is what 例1
affects the time complexity . Finally, define the time complexity of Example 1 as .最大
for循环
O(N)
1.1. Common time complexity cases
Common time complexity: O ( 1 ) O(1)O(1)、 O ( l o g n ) O(logn) O(logn)、 O ( n ) O(n) O(n)、 O ( n l o g n ) O(nlogn) O ( n l o g n )、O ( n 2 ) O(n^2)O ( n2 )
[Example 2:O(1) O(1)O(1)】
def o1(num):
i = num # 执行时间为:a;
j = num * 2 # 执行时间为:b;
return i + j # 执行时间为:c;
# 所以o1类的时间复杂度 = a + b + c,为常量,和num的个数无关
Define the time complexity of Example 2 as O(1)
.
[Example 3: O ( N ) O(N)O ( N )】
def ON(num):
total = 0 # 执行时间为:a;
for i in range(num):
total += i # 执行时间为:b;num次为:num * b;
return total # 执行时间为:c;
# 所以test类的时间复杂度 = a + num * b + c,和num的个数有关
Define the time complexity of Example 3 as O(N)
.
[Example 4: O ( log N ) O(logN)O(logN)】
def OlogN(num):
i = 1 # 执行时间为:a;
while (i < num):
total = i * 2 # 执行时间为:b;X次为:num = i*2^X,X = log2(num/i)
return i # 执行时间为:c;
# 所以test类的时间复杂度 = log2(num/i),和num的个数有关,因为i为常数,可忽略
Define the time complexity of Example 4 as O(logN)
.
[Example 5: O ( N + M ) O(N + M)O ( N+M)】
def ON(num1, num2):
total = 0 # 执行时间为:a;
for i in range(num1):
total += i # 执行时间为:b;num次为:num1 * b;
for i in range(num2):
total += i # 执行时间为:b;num次为:num2 * b;
return total # 执行时间为:c;
# 所以test类的时间复杂度 = a + num1 * b + num2 * b + c,和num的个数有关
Define the time complexity of Example 5 as O(N + M)
.
【例6:O ( N log N ) O(NlogN)O(NlogN)】
def OlogN(num):
i = 1 # 执行时间为:a;
for i in range(num1): # 执行 num1次
while (i < num2):
total += i + j # 执行时间为:b;X次为:num2 = i*2^X,X = log2(num2/i)
j = j * 2
return i # 执行时间为:c;
# 所以test类的时间复杂度 = (num1)*log2(num2/i),和num的个数有关,因为i为常数,可忽略
Define the time complexity of Example 4 as NO(logN)
.
[Example 5: O ( N + M ) O(N + M)O ( N+M)】
def ON(num1, num2):
total = 0 # 执行时间为:a;
for i in range(num1):
for i in range(num2):
total += i + j # 执行时间为:b;num1 * num2次为:num1 * num2 * b;
return total # 执行时间为:c;
# 所以test类的时间复杂度 = a + num1 * num2 * b + c,和num的个数有关
Define the time complexity of Example 4 as O ( N 2 ) O(N^2)O ( N2)。
1.2. Time complexity comparison chart
Time complexity in descending order: O ( N ! ) O(N!)O(N!) > O ( 2 N ) O(2^N) O(2N) > O ( N 2 ) O(N^2) O ( N2) > O ( N l o g N ) O(NlogN) O(NlogN) > O ( N ) O(N) O(N) > O ( l o g N ) O(logN) O(logN) > O ( 1 ) O(1) O(1)
space complexity
1. What is space complexity?
算法的存储空间与算法的输入值之间的关系
(The space is occupied by the variables we declare)2. Big
O
representation?
O(N)
, whereN
refers to例1
innum
,N = num
1.1. Common space complexity cases
There are two commonly used space complexities.
understand
Space complexity generally refers to disk space, so time complexity is generally given priority in work.