-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathO(n).py
36 lines (31 loc) · 1.21 KB
/
O(n).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import time
nemo = ['nemo']
everyone = ['dory', 'bruce', 'marlin', 'nemo', 'gill', 'bloat', 'nigel', 'squirt', 'darla']
large = ['nemo' for i in range(100000)]
def find_nemo(array):
t0 = time.time()
for i in range(0,len(array)):
if array[i] == 'nemo':
print("Found Nemo!!")
t1 = time.time()
print(f'The search took {t1-t0} seconds.')
find_nemo(nemo)
find_nemo(everyone)
find_nemo(large)
def funchallenge(input):
temp = 10 #O(1)
temp = temp +50 #O(1)
for i in range(len(input)): #O(n)
var = True #n*O(1)
temp += 1 #n*O(1)
return temp #O(1)
funchallenge(nemo)
funchallenge(everyone)
funchallenge(large)
#Total running time of the funchallenge function =
#O(1 + 1 + n + n*1 + n*1 + n*1 + 1) = O(3n +3) = O(3(n+1))
#Any constant in the Big-O representation can be replaced by 1, as it doesn't really matter what constant it is.
#Therefore, O(3(n+1)) becomes O(n+1)
#Similarly, any constant number added or subtracted to n or multiplied or divided by n can also be safely written as just n
#This is because the constant that operates upon n, doesn't depend on n, i.e., the input size
#Therefore, the funchallenge function can be said to be of O(n) or Linear Time Complexity.