Order of a recursive function
Hi all,
Can anybody tell me how to calculate the order of a recursive function.
Lets take factorial function.
Without using recursion
int x =1;
for(int i = 1;i<=n;i++)
x = x*i;
In this case the order will the O(n) in terms of time and in terms of space it will be O(1).
Assuming I am right. Correct me otherwise.
Now suppose I implemet the same factorial with rtecursion
return(n==0? 1 : n * fact(n-1));
What will be the order in time and space????
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
Well for a value of 'n', how many recursive calls will there be?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
There will be n recursive calls, so the order will be O(n ) for time complexity. Am i right?
In that case space complexity will also be of O(n)
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
i need help please...i ask, to make a program that accepts a positive integers and displays all odd number between 1 to the integer entered (inclusive) accept the integer in main() and use a recursive function to identify and display the odd numbers.
tnx..salie
Don't resurrect old threads to just ask your question, you've already your own thread here: http://www.daniweb.com/forums/post906282.html .
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243