In order to find Big O for a recursive algorithm, it is needed to know the stopping criteria of that algorithm. For the recursive algorithm to find Factorial of a number it is very easy to find the stopping criteria.
int fact(int n)
{
if((n==1)||(n==0)) return 1;
else retrun n*fact(n-1);
}
And we can make a mathematical definition of this problem as follow:
$ fact(n)= \begin{cases} 1,& \text{if } n = 1 \vee n=0\\ n*fact(n-1), &{otherwise} \end{cases} \tag{1} $
Now my question is how is the correct mathematical definition for this algorithm:
void func(int n)
{
if(n>0)
{
func(n-1);
}
else
{
print(n)
}
}
Also, for the same algorithm if I change it as follow what is the stopping criteria and the mathematical definition of it?
int n = 5;
int rnd; // Random number between 0 and n
void func(n)
{
if(n>rnd)
{
func(n-1);
}
else
{
print(n)
}
}
Now the stopping criteria is random? So how can I define this in order to find Big O?