Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Way to do recursion in Java in a very efficient manner

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

 
Marshal
Posts: 73725
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 73725
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
He baked a muffin that stole my car! And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic