public static int[][] permutations(int[] a) {
        int permutations[][] = new int[1][a.length];
        int[] tempCopy, aCopy;
        boolean foundAk;
        int k = 0, l = 0, n, p = 0, al, ak, r;
        a = bubbleSortIncreasing(a); //reset the array for algorithm
        permutations[p++] = Arrays.copyOf(a, a.length);
        aCopy = permutations[0];
        for (; p < factorial(aCopy.length); p++) {
            foundAk = false; //reset variable
            //Step 1 find k such that a[k] < a[k+1]
            for (ak = 0; ak < a.length - 1; ak++) {
                if (aCopy[ak] < aCopy[ak + 1]) {
                    foundAk = true;
                    k = ak;
                }
            }
            if (foundAk) {
                //Step 2 find the largest l such that a[k] < a[l]
                for (al = k + 1; al < aCopy.length; al++) {
                    if (aCopy[k] < aCopy[al]) {
                        l = al;
                    }
                }
                //Step 3 swap a[k] with a[l]
                ak = aCopy[k];
                aCopy[k] = aCopy[l];
                aCopy[l] = ak;
                //Step 4 reverse sequence from a[k+1] inclusively to final element
                tempCopy = Arrays.copyOfRange(aCopy, k + 1, aCopy.length);
                //revisit loop to use the numbers from tempCopy properly
                for (r = k + 1, n = tempCopy.length - 1;
                        r < aCopy.length && n >= 0; r++, n--) {
                    aCopy[r] = tempCopy[n];
                }
                permutations = arrayAddLine(permutations, aCopy);
            }
        }
        return permutations;
    }