• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion  RSS feed

 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja Preuss and ranchers
I have some psudo-code that describe a recusion, but I do not know whether it will stop or not. Is it missing the base case?

thanks for your time
Regards
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I read this, Reverse() calls itself every time it is called, so no, it will never stop. Are there some missing braces that belong with the "if" statement?
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ernest
I do not know too ^o^ as I just copy the answer given from TA and then smile
I think that only version 1 works
Version 1

Version 2

Version 3 (Original version) ambiguous one

Do the above three version work?
I wonder that is the program missing the base case?
thanks ^o^
[ December 11, 2004: Message edited by: siu chung man ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For version 3, I think you need to establish where the braces should be. This is pseudocode after all, so probably you're expected to infer the block structure from indentation. I suspect it's intended to be:

For all three of these, you should be able to determine what happens by analysis. For each one: what happens if Q has length 0? What about length 1? Or length 2? Of course, if an algorithm is broken for 0 or 1, you don't really need to analyze any more, I think.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!