// Arup Guha
// 7/22/2015
// Sample Permutation Code

public class perm {

    final public static int SIZE = 5;

    // Call wrapper method.
    public static void main(String[] args) {
        printPerm(SIZE);
    }

    // Just a wrapper method for the recursive method.
    public static void printPerm(int n) {
        int[] order = new int[n];
        boolean[] used = new boolean[n];
        printPermRec(order, used, 0);
    }

    public static void printPermRec(int[] order, boolean[] used, int k) {

        // Filled in permutation, print it.
        if (k == order.length) print(order);

        // Try each possible item in slot k, that hasn't been used yet.
        for (int i=0; i<order.length; i++) {
            if (!used[i]) {
                order[k] = i;
                used[i] = true;
                printPermRec(order, used, k+1);
                used[i] = false;
            }
        }
    }

    // Prints items in array, in order.
    public static void print(int[] array) {
        for (int i=0; i<array.length; i++)
            System.out.print(array[i]+" ");
        System.out.println();
    }
}
