• Post Reply Bookmark Topic Watch Topic
  • New Topic

Way to do recursion in Java in a very efficient manner  RSS feed

 
om ganesh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to encode recursion using Java.

I would like to know an efficient approch where I can bring down the nummber of variables being passed as method parameters. - Without adding to the Method Call Stack - I basically would like to come up with an approach which would make use of least number of local variables per method call.

for ex
In merge sort we basically use recursion, basically call recursion using divide & conquer technique.

but in worst case scenario if there ar many number of elements there would be lot of variables added to the method call stack.

is there a way around this ?

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't add many things to the call stack in a merge sort. Remember a merge sort only goes to an average depth of log2(n) where n is the number of elements in the array to sort.
You usually pass few arguments; merge sort is one of the worst offenders where you pass at least three arguments, but for a lot of recursions you pass less. For example, factorial only needs one argument and Fibonacci will run in linear time with two arguments.

You ought not to be thinking about memory use, but rather about how your recursion will run in efficient complexity. There is a 1-argument version of Fibonacci which runs in exponential complexity, a 2-argument version which runs in linear time and a 4-argument version (Anne Kaldewaij Programming : the derivation of algorithms Prentice Hall International series in computer series Hemel Hempstead : Prentice Hall International, 1990. ISBN 0132041081, page 98) which runs in logarithmic time.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
om ganesh wrote:I would like to know an efficient approch where I can bring down the nummber of variables being passed as method parameters. - Without adding to the Method Call Stack - I basically would like to come up with an approach which would make use of least number of local variables per method call.


You can make sure that you don't pass constants in the recursive call. Constants could be kept outside the recursive method and be accessed as global variables.

On the other hand the method call stack is extremely efficient. I would say it's impossible to match it using some memory management of one's own.
 
om ganesh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks uj nossnahoj & Campbell Ritchie

uj noosnahoj - will be trying to write it, will keep you folks posted on it.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!