Hey I am a private tutor in Python for students in the "Intro to CS" class in a nearby university. My student gave me a question from a test about calculating the time complexity (Big O Notation) of some function, and I am having a hard time myself coming up with a good answer and explanation to how I got it. This is the function:
def oh(lst, n):
if n==1:
print(lst)
return
for i in range(n):
oh(lst, n-1)
if n%2==0:
lst[i], lst[n-1] = lst[n-1], lst[i]
else:
lst[0], lst[n-1] = lst[n-1], lst[0]
Now the way I see it, it's supposed to be something along the lines of n**(n-1) or something big like that, since every recursive call makes n calls to the next recursion, I tested the function with a wrapper to see how it runs for n=10 and some lst with 10 ints, and count the operations manually and got something that vaguely fits my assumption (about 15 million operations), but it's not precise enough..
oh(lst, 4), each of which will do 4 passes ofoh(lst, 3)and so on.range(n), wherendecreases by 1 with each recursive call. looks to be O(n!)