Reputation: 11
I need a little help on figuring out the Big-Theta running time for this function.
int recursive(int n) {
sum = 0;
for (int i = 1; i <= n; i++)
sum++
if (n > 1)
return sum + recursive(n-1);
else
return n;
}
I know how what the run time of this function would be if the for loop wasn't in the function, but the loop is throwing me off a little bit. Any advice?
Upvotes: 0
Views: 712
Reputation: 359786
for
loop, not recursive, the function would be O(n)
.for
loop, it would also be O(n)
.n
recursive steps (which we know is O(n)
) and it's got an O(n)
for
loop at each of the n
steps.So... does that help?
Upvotes: 1