• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Permutation  RSS feed

 
daniel Ishibashi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am new to JAVA.
I wanna write a Permutation code.
For example, I have [1,2,3]. I want to print 123,132,213,231,312,321

Below is my code:


However, it will print 123,132,312,321,123,132.

Actually, I use the algorithm from "Data Structures with C++ Using STL" by WIlliam Ford and WIlliam Topp. In this book, there is a section "Listing Permutations" described this algorithm.

So, why is it wrong?

Thanks.

[Update: Added code tags (Read UseCodeTags) ]
 
Andi Eska
Greenhorn
Posts: 14
BSD Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to pass create a copy of the original passed values otherwise you perform the same permutations on the permutated values
which produces the duplicates in your result.

simply create a copy at the passed values as the beginning of your permute method, like


And the result will be:123,132,213,231,312,321
as expected.


 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!