Big-O Complexity#
Constant Time#
len(list)
.
This is a very simple algorithm becausethelistclass maintains, for each list, an instance variable that records the currentlength of the list.
This allows it to immediately report that length, rather than taketime to iteratively count each of the elements in the list.
Using asymptotic notation,we say that this function runs in data[j]
.
The indexing operation also has constant-time complexity.
Logorithm Time#
Linear Time#
fun_max2
is used to find the maximum value from a given list.
L2
is an indexing operation and an assignment, and both operations are constant-time L3
is a for loop, which executes n
times (n
is the length of the given list), and needs L6
is a return operation, which needs
1 def find_max2(l):
2 max_l = l[0]
3 for element in l:
4 if element>max_l:
5 max_l = element
6 return max_l
n-log-n Time#
Qudratic Time#
prefix averages
of a sequence of numbers.
Given a sequence
The following algorithm prefix_average1
renders a comlexity of
L2
executes in constant time.L3
initializes a Python list with length of with entries of 0. This uses a constant number of primitive operations per element and thus run in time.L4
is an outer loop that executes times. The body of the outer loop inL5, L8
executes in constant time.L6, L7
is an inner loop that execute times, depending the current counter . ThusL7
executes times in total within the outer loop, which is for .Thus, the code in toal is
, and considerd as .
1 def prefix_average1(S):
2 n = len(S)
3 A = [0.]*n
4 for j in range(n):
5 total = 0
6 for i in range(j+1):
7 total += S[i]
8 A[j] = total/(j+1)
9 return A
The above algorithm can be improved to prefix_average2
.
L2
andL4
are constant time.L3
runs in time.L5
executes times, with constant time operations inL6
andL7
.Thus, the code in total is
.
1 def prefix_average2(S):
2 n = len(S)
3 A = [0.]*n
4 total = 0
5 for j in range(n):
6 total += S[j]
7 A[j] = total/(j+1)
8 return A
Cubic Time#
three-way set disjointness
problem.
Suppose we are given three sequences of numbers, disjoint1
as follows, where the complexity is
1 def disjoint1(A,B,C):
2 for a in A:
3 for b in B:
4 for c in C:
5 if a==b==c:
6 return False
7 return True
We can improve the complexity to disjoint2
.
The idea is that we don’t need loop over C if we already know
L2
manages an outer loop over A, which requires time.L3
loops over B, which accounts for a total of since that loop is executed different times.L4
is exectued times.L5-L7
depends on the number of matching pairs , whose maximum is . Therefore, this block will at most be executed times.
1 def disjoint2(A,B,C):
2 for a in A:
3 for b in B:
4 if a == b:
5 for c in C:
6 if a == c:
7 return False
8 return True