• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

recursion

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
I have been working on a specific algorithm for some time now and am still unsuccessful. The program is to accept one number as input and then must generate all possible orders of the values from 1 to the input number. This must be done using recursion. Any ideas? thanks
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us some of your code.
 
Cosmo Kramer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Here is some of my experimental code
public static int[] Recursion(int[] unused, int[] used, int l, int n)
{
l = 1;
int i = 0;
if(l > n)
{
for(i = 0; i < n; i++)
{
return used[i];
}
} else {
for(i = 0; i < n; i++)
{
if(unused[i] == 1)
{
unused[i] = 0;
used[l] = i;
}
}
return Recursion(unused, used, l + 1, n);
}
}
}
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I understand your requirements. I can't say I have a solution just yet. This isn't a specific JAVA problem, but more like a Data Structures and Algorithm problem. Unfortunately, we don't have a DS&A forum.
Have you tried coming up with a solution without using recursion? This might be easier. If you do get it working without recursion, it might be easy to figure out how to apply the recursive routine from a working solution.
My $0.02,
-Peter
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cosmo,
Here is a sample that may help you figure out a more eligant solution. I am assuming that this is chance for you to learn more about recurision, so below is not what I would considered a "final" solution.

steve
 
reply
    Bookmark Topic Watch Topic
  • New Topic