算法:全排列


代码例如以下:

package com.huan;

import java.util.Arrays;

public class Permutation {
	public static void main(String[] args) {
		final int SIZE = 3;
		int[] data = new int[SIZE];
		for (int i = 1; i <= SIZE; i++){
			data[i-1] = i;
		}
		
		recursive(data, SIZE-1);
	}
	
	public static void recursive(int[] data,int index){
		if(index == 0){
			System.out.println(Arrays.toString(data));
		}
		for (int i = index; i >= 0 ; i--){
			swap(data, index, i);
			recursive(data, index-1);
			swap(data, index, i);
		}
	}
	
	public static void swap(int[] data, int i, int j){
		int temp = data[i];
		data[i] = data[j];
		data[j] = temp;
	}
}




posted @ 2017-08-08 12:38  wzzkaifa  阅读(183)  评论(0编辑  收藏  举报