I was just wondering how I can find the time complexity of my recursive function here. I know that for the most part of my program its all in constant time O(1) but how do I calculate the time complexity of recursion functions as well.
int pal(int n, int temp)
{
// Using division of 10 trick
if(n == 0)
return temp;
// Build the number backwards.
temp = (temp * 10) + (n % 10);
// Then advance through the number.
return pal(n / 10, temp);
}
int main()
{
int n = 5678;
int temp = pal(n, 0);
if(temp == n)
cout << "Yes it is a palindrome";
else
cout << "No its not a palindrome";
]
It's O(log(n))
, where n is the number.
But it's also O(s)
, shere s is the input size (pretty standard in computation theory to use input size as a string)
Compiled on any existing C++ platform, you could also argue it is O(k)
because int
's have finite size.
So, it depends on the convention you use. Or whatever your professor showed in class. :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.