Have a question about a Quiz 1 answer... In problem 7.2, it asks, "Under the assumption that logBase2 is O(n), what is the order (use big Oh notation) of f(n)?" The answer says that it is still O(n), but it seems like it should be O(nlogn), as f(n) has a loop which executes as many times as the logBase2(n) value. Therefore, the call to logBase2(n) will be O(n), and the loop is O(log(n)), so shouldn't the entire function be O(nlogn)?
show
Okay, I hadn't seen Recitation 5 that goes over the answers. The reason that Problem 7.2 on Quiz 1 isn't O(nlogn) is because the O(logn) section of the code is additive with the O(n) section, and so the O(n) dominates. If the program were written in such a way that the O(logn) section were *inside* the code performing the O(n) function, then the entire piece of code would, indeed, be O(nlogn).
Join our real-time social learning platform and learn together with your friends!