Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all permutations in Java [duplicate]

Possible Duplicate:
Generating all permutations of a given string

I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?

like image 468
chimeracoder Avatar asked May 03 '11 23:05

chimeracoder


1 Answers

Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.

like image 80
Jérôme Verstrynge Avatar answered Oct 20 '22 00:10

Jérôme Verstrynge