Java实现——数组元素全排列

转自几个面试经典算法题Java解答(http://www.cnblogs.com/sunniest/p/4596182.html)题目三

打印元素各不相同的字符数组的全排列

public class AllSort {
	public void permutation(char[] c , int begin , int end){
		if(begin == end){
			for(int i = 0 ; i < c.length ; i++){
				System.out.print(c[i]);
			}
			System.out.println();
		}else{
			char temp;
			for(int i = begin ; i <= end ; i++){
				temp = c[begin];
				c[begin] = c[i];
				c[i] = temp;
				
				permutation(c , begin + 1 , end);
				
				temp = c[begin];
				c[begin] = c[i];
				c[i] = temp;				
			}
		}
	}
	public static void main(String[] args) {
		AllSort a = new AllSort();
		char[] c = {'a' , 'b' , 'c' , 'd'};
		a.permutation(c, 0, c.length - 1);
	}
}


你可能感兴趣的:(Java,算法,数组)