Now I want to get all permutations where n (e.g.2) numbers are taken from setA and m (e.g.4) numbers are taken from setB.

Does someone have an efficient algorithm (preferable implemented in Java) which allows to generate all porssible permutations?

Peter

You just need a function that will produce all permutations of n+m distinct elements. Then put that inside a loop that generates all the combinations of n distinct elements from set 1 and m distinct elements from set 2. This requires just a simple double loop.

If you regard 1, .. n+m as the indices into the array of elements of a set A of size n+m (note I am using unity based arrays here) you need only produce permutations of the index array to get your permutations of the set. There is pre-existing code out there somewhere that will produce all the permutations of [1, .. , n+m] in order.