Home arrow static arrow Java Programming [Archive] - Permutation
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Permutation
This topic has 36 replies on 3 pages.    1 | 2 | 3 | Next »

Posts:357
Registered: 8/5/04
Permutation  
Aug 8, 2004 5:06 PM



 
Hello,
Below is permutation generator method,

no output has same no. twice or more in one line, i.e. each no. occurs only once in a line

 public int[] nextPermutation(int n)   {        int[] p = new int[n];      for (int i = 0; i < n; i++)          {         p[i] = i + 1; //contain 1...10.             }      int pSize = n; // max = 10       int[] r = new int[n]; //size = 10            for (int i = 0; i < n; i++)      {  int pos = generator.nextInt(pSize);                   r[i] = p[pos]; // call made to random index in a array of size n to store nos. in r[i]         //I CAN'T get whats the MAIN ROLE of the two statements below         p[pos] = p[pSize - 1]; //overwrites already stored ints with some other nos.          pSize--; //       }      return r;   }
 

Posts:24,036
Registered: 2/3/03
Re: Permutation  
Aug 8, 2004 5:10 PM (reply 1 of 36)



 
//I CAN'T get whats the MAIN ROLE of the two
statements below
p[pos] = p[pSize - 1]; //overwrites already stored ints with some other nos.
pSize--;

The main role is to set the value of p[pos] to the value of p[pSize - 1], then decrement the pSize variable.

Hope this helps.
 

Posts:826
Registered: 7/1/03
Re: Permutation  
Aug 8, 2004 5:11 PM (reply 2 of 36)



 
Hi Annihilator,
Your iterators don't match your returns.
/k1
 

Posts:357
Registered: 8/5/04
Re: Permutation  
Aug 8, 2004 5:13 PM (reply 3 of 36)



 

The main role is to set the value of p[pos] to the
value of p[pSize - 1], then decrement the pSize
variable.

Thanks, i can also see that but how will it change the values in
 r[i] 
, so that each line has only one occurence of a no.
 

Posts:826
Registered: 7/1/03
Re: Permutation  
Aug 8, 2004 5:15 PM (reply 4 of 36)



 
You need to reset the arrays to do that.
 

Posts:357
Registered: 8/5/04
Re: Permutation  
Aug 8, 2004 5:19 PM (reply 5 of 36)



 
You need to reset the arrays to do that.

never heard of resetting the arrays?
 

Posts:826
Registered: 7/1/03
Re: Permutation  
Aug 8, 2004 5:20 PM (reply 6 of 36)



 
Arraya.reset()
/k1
 

Posts:357
Registered: 8/5/04
Re: Permutation  
Aug 8, 2004 5:23 PM (reply 7 of 36)



 
do I don't need that this implementation is working precisely...
i don't need to meddle with anything else

i just want to know the "purpose" of those two statements. please

thanks if replies ASAP
 

Posts:12,831
Registered: 2/22/00
Re: Permutation  
Aug 8, 2004 5:25 PM (reply 8 of 36)



 

but how will it change the
values in
 r[i] 
, so that each line has
only one occurence of a no.

I would think that its purpose is to keep the p array clean, and it'll effect r only in the next iteration of the loop.
 

Posts:826
Registered: 7/1/03
Re: Permutation  
Aug 8, 2004 5:25 PM (reply 9 of 36)



 
Surely, You time is definitely valuable.

Did you check out Arraya.reset() that would solve it?

/k1
 

Posts:357
Registered: 8/5/04
Re: Permutation  
Aug 8, 2004 5:26 PM (reply 10 of 36)



 
yeah, but how does it "gurantee" the uniqueness of nos. stored in
r[i] 
?
 

Posts:826
Registered: 7/1/03
Re: Permutation  
Aug 8, 2004 5:28 PM (reply 11 of 36)



 
Thats what Arraya.reset() is for.
Did you check the javadoc?
/k1
 

Posts:24,036
Registered: 2/3/03
Re: Permutation  
Aug 8, 2004 5:30 PM (reply 12 of 36)



 
i just want to know the "purpose" of those two
statements. please

Oh, thanks for clarifying. The purpose of those two statements is to set the value of p[pos] to the value of p[pSize - 1], then decrement the pSize variable by one.


thanks if replies ASAP

Hope this helps.
 

Posts:826
Registered: 7/1/03
Re: Permutation  
Aug 8, 2004 5:31 PM (reply 13 of 36)



 
And i was having fun yaw.
You went and spoiled it :)
/k1
 

Posts:24,036
Registered: 2/3/03
Re: Permutation  
Aug 8, 2004 5:31 PM (reply 14 of 36)



 
yeah, but how does it "gurantee" the uniqueness of
nos. stored in r[i]?

cuz it decrements the pSize variable by one. u will understand it now thru vulcan mind-meld.
 
This topic has 36 replies on 3 pages.    1 | 2 | 3 | Next »