If iterating over all permutations is enough for you, see this answer: Stepping through all permutations one swap at a time . For a given n the iterator produces all permutations of numbers 0 to (n-1). You can simply wrap it into another iterator that converts the permutation of numbers into a permutation of your array elements. (Note that you cannot just replace int[] within the iterator with an arbitrary array/list. The algorithm needs to work with numbers.)
2 of 2
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Petr
- 63.6k
- 13
- 162
- 333