deffind_max(data):# Return the maximum element from a nonempty Python list biggest = data[0]# The initial value to beatfor val in data:# For each value:if val > biggest:# if it is greater than the best so far, biggest = val # we have found a new best (so far)return biggest # When loop ends, biggest is the max
initialization: O(1)
loop: O(n)
return: O(1)
To sum up, this algorithm has O(n) time complexity.
defprefix_average1(S):# Return list such tath, for all j, A[j] equals average of S[0], ..., S[j] n =len(S) A = [0]*n # Create new list of n zerosfor j inrange(n): total =0# begin computing S[0]+...+S[j]for i inrange(j+1): total += S[i] A[j]= total / (j+1) # record the averagereturn A
The running time of prefix_average1 is O(n2)
defprefix_average2(S): n =len(S) A = [0]*nfor j inrange(n): A[j]=sum(S[0:j])/(j+1)return A
This big-Oh notation is used widely to characterize running times and space bounds in terms of some parameter n. (prefix_average2 is also O(n2))
defprefix_average3(S): n =len(S) A = [0] * n total =0for j inrange(n): total += S[j] A[j]= total / (j+1)return A
The above expression only has O(n)time complexity.
Time complexity in Python
len(data): O(1)
data[j]: O(1)
Python's lists are implemented as array-based sequences.