I’m confused about how Big-O works when dealing with functions within functions (when analyzing worst case). For example, what if you have something like:
for(int a = 0; a < n; a++)
{
*some function that runs in O(n*log(n))*
for(int b = 0; b < n; b++)
{
*do something in constant time*
}
}
Would this entire block run in O(n^2*log(n)), because within the first for loop, you have an O(n) and an O(n*log(n)), so O(n*log(n)) is greater, and therefore the one we take? Or is it O(n^3*log(n)) because you have an O(n) and an O(n*log(n)) within the outer for loop?
Any help is appreciated! Thanks!
It’s
Because you have
O(N)iterations of anO(N lg N)function andO(N)constant time operations. TheO(N lg N) + O(N)simplifies toO(N lg N)becauseO(N lg N) > O(N).