programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Liutauras Vilda
Sheriffs:
• Paul Clapham
• Jeanne Boyarsky
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Piet Souris
• Carey Brown
Bartenders:
• Jesse Duncan
• Frits Walraven
• Mikalai Zaikin

# Way to do recursion in Java in a very efficient manner

Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
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 ?

Marshal
Posts: 75874
361
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 237
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Thanks uj nossnahoj & Campbell Ritchie

uj noosnahoj - will be trying to write it, will keep you folks posted on it.

Campbell Ritchie
Marshal
Posts: 75874
361
• Number of slices to send:
Optional 'thank-you' note:
You're welcome

 pie. tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton