I am working on figuring out big O notation for algorithms and I was wondering if I have this down correctly.
I am currently analyzing the following code:
int expon(int x, int n) /* n > 0 */
{
if (n==0) return 1;
else
{
if even(n) return expon(x*x, n/2);
else
return expon(x, n-1)*x;
}
}
This is what I have so far: The first if statement where it checks to see if n = 0 is simply a constant all it gets is c.
The second if statement where the even(n) check is called does this n times, thus receiving an n and the return expon(x*x, n/2) does this n amount of times also receiving an n. So that if statement is n^2. The final statement where it handles everything else only executes once, so we can call that c.
Finally we add this all up and we get: c+c(n^2). And if we want to write this in bigO notation, we would drop the constants and simply write O(n^2).
Could anyone correct me if I am wrong here? I feel like I am not analyzing this correctly (especially the second if) as well as not adding/multiplying the n's and c's total correctly.
Thanks a bunch!