import java.util.ArrayList;
public class PrintPerms {
private static int cnt = 0;
public static void main(String[] args) {
int n = 5;
// adding all elements in the list we want to pass as argument
ArrayList<Integer> listToPermute = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
listToPermute.add(i);
}
// starting with a depth of 0
printPerms(listToPermute, "", 0);
}
private static String printPerms(ArrayList<Integer> listToPermute, String prefix, int depth /* this is for debug only */) {
String str = "";
for (Integer outerElem : listToPermute) {
ArrayList<Integer> newListToPermute = new ArrayList<Integer>();
for (Integer innerElem : listToPermute) {
if(innerElem != outerElem) {
newListToPermute.add(innerElem);
}
}
// prefix will be updated by using the outerElem
String newPrefix = prefix + outerElem + " -- ";
str = outerElem + " -- " + printPerms(newListToPermute, newPrefix, depth+1);
if(listToPermute.size() == 1) {
// this indicates leaf condition
System.out.println(cnt + ": " + prefix + str);
cnt++;
}
}
return str;
}
}