What are the time complexities of the following code? I posted this on the general stackexchange website, but it was suggested that I post it here.
def func(n):
for _ in range(n):
if n == 4:
for _ in range(n):
<O(1) operation>
It will only be O(n^2) for one specific input (n = 4) but is O(n) for all other inputs. In this case the worst case is obviously O(n^2), yet my instructor says that O(n) is the correct answer. If "big-Oh" notation is to indicate the worst case scenario, why is it not O(n^2)?
Another one is:
def func2(n):
for _ in range(n):
if n%2 == 0:
for _ in range(n):
<O(1) operation>
I am not so certain about the run time of this piece of code. Again, worst case is O(n^2). This time half of all possible inputs results in the worst case. Would this suffice in saying that the code runs in O(n^2) time?
If the first part is O(n) and the second part is O(n^2), is there a general rule of thumb when you choose the truly worst case for the "big-Oh" representation?