全排序-递归算法

全排序-递归算法

public class Permutation {

    /*
     * 
     */
    public static void main(String[] args) {
        int array[] = new int[5];
        for(int i=0;i<5;i++) {
             array[i] = i+1;
        }
        System.out.println("str:"+Arrays.toString(array));

        Permutation.fullPermutation(array, 0, array.length-1);


    }


    public static void fullPermutation(int []array,int cursor,int end) {

        if(cursor == end) {
            System.out.println(Arrays.toString(array));
        } else {
            for(int i=cursor;i<=end;i++) {
                Permutation.swap(array, cursor, i);
                fullPermutation(array, cursor+1, end);
                //交换完后,要交换回来
                Permutation.swap(array, cursor, i);
            }
        }

    }

    /*
     * 交换数据
     */
    private static void swap(int[] array,int cursor,int index) {
        int temp = array[cursor];
        array[cursor] = array[index];
        array[index] = temp;
    }

}

你可能感兴趣的:(Java,数据结构与算法)