om ganesh

Greenhorn

Posts: 18

posted 8 years ago

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 ?

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

posted 8 years ago

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(

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.

*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

posted 8 years ago

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 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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |