Reputation: 15
What is the time complexity and Big-O notation for this code?
def rot(a,n):
for i in range(n-1):
temp=a[i]
a[i]=a[i+1]
a[i+1]=temp
return a
n=int(input())
x=[1,2,3,4,5,6,7]
for i in range(n):
x=rot(x,7)
print(x)
Upvotes: 0
Views: 31
Reputation: 5630
The order is O(n)
You have two nested for loops, which might indicate it's O(n**2)
, but the second for loop does not depend on the variable n. it depends on x and 7, which both do not depend on n.
The parameter of the function rot is called n
, but its passed value is always 7
, thus not depending on n.
So if you multiply n by 2 the amount of instructions to execute is about twice as much.
Upvotes: 1