Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

problem with recurssion

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i am trying to generate the permutatios of a given set of numbers
i am using recursion to get the permutations i am supposed to move the largetst number to the start and terminate generating the permutation when the maximum number reaches the first position and i have to display the permutations generated in each call in the called environment
i am placing the code here the problem with it is it can display only the last permutation
i really appreciate if some one can help me
thanks in advance


public class Test{
public static int start=1;


static void main(){


int[] array=new int[10];
for(int i=0;i<10;i++)
array[i]=i;


int comparions=0;
int count=0;

do{


System.out.print("\n");

for(int i=0;i<array.length;i++)
System.out.print(array[i]+"\t");

}while(nextPermutation(array));
}
public static boolean nextPermutation(int[] a)
{


int maxindex=0;
for(int i=0;i<a.length;i++)
{
if(a[maxindex]<a[i])
maxindex=i;
}
if(maxindex==0)
return false;
else
{
int temp=a[maxindex];
a[maxindex]=a[maxindex-1];
a[maxindex-1]=temp;
nextPermutation(a);

return true;
}

}
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The recursion probably works, but you can't see it, because there is nothing in your recursion to print it out. Since there are 3628800 different permutations of 10 numbers, maybe it is just as well.
CR
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think there is something I have missed going wrong.
Try the Campbell method for testing the flow of control through a program. Put a print statement in each method. I would suggest something like

Then you can see where your code is going.

I have tried it, and found out you are not actually doing what you think. Follow the 9 and see what happens to it.

CR
 
vinay kumar samineni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Actually, I think there is something I have missed going wrong.
Try the Campbell method for testing the flow of control through a program. Put a print statement in each method. I would suggest something like

Then you can see where your code is going.

I have tried it, and found out you are not actually doing what you think. Follow the 9 and see what happens to it.

CR



i did check this way what was happening was it is passing the just the result of th elast execution of the function
can some one please help me out i am desperate
actually what i have do do is some thing like this



public class Test{



static void main(String[] args){


int[] array=new intp[]{0,1,2,3,4,5,6,7,8,9,10};


int comparions=0;
int count=0;

do{
//make a copy or original array:

arrayCopy=..

comparions=MergeClass.mergesort(arrayCopy);

total+=comparions;

count++;


//The method (nextPermutation) re-arranges the array elements as a a set of
//next permutation values.
//Returns false when permutations finishes.And loop ends

}while(RecPermutation.nextPermutation(array);


..println("Average: " + total/count);


}



}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The problem with your program is that the loop only executes ONE TIME.
That's because your recursive method keeps executing until the biggest number is in index 0 and thus returns false.
Just take out one line and your program will work.

public class Test{
public static int start=1;

public static void main(String[] args){

int[] array=new int[10];
for(int i=0;i<10;i++)
array[i]=i;


int comparions=0;
int count=0;

do{


System.out.print("\n");

for(int i=0;i<array.length;i++)
System.out.print(array[i]+"\t");

}while(nextPermutation(array));
}
public static boolean nextPermutation(int[] a)
{


int maxindex=0;
for(int i=0;i<a.length;i++)
{
if(a[maxindex]<a[i])
maxindex=i;
}
if(maxindex==0)
return false;
else
{
int temp=a[maxindex];
a[maxindex]=a[maxindex-1];
a[maxindex-1]=temp;
//nextPermutation(a) (JUST COMMENT OUT THIS LINE)
return true;
}
}

}
 
I don't always make ads but when I do they're tiny
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic