フル配列実装
ぜんはいち
キー:array[0...n]の全配列はPerm(array,(0...n)=array[i]とPerm(array[0...i-1,i+1 n))の組み合わせ
キー:array[0...n]の全配列はPerm(array,(0...n)=array[i]とPerm(array[0...i-1,i+1 n))の組み合わせ
- public class TestPerm {
- public static void main(String[] args){
- test1();
- }
- public static void test1(){
- String str = "abcde";
- char[] array = str.toCharArray();
- TestPerm.perm(array, 0, array.length);
- System.out.println("Total:"+TestPerm.count);
- }
- private static int count = 0;
- public static void perm(char[] array, int start, int end){
- if(start==end){
- count++;
- System.out.println(array); //
- return;
- }
- for (int i = start; i < end; i++) {
- swap(array, start, i);
- perm(array, start+1, end);
- swap(array, start, i);
- }
- }
-
- private static void swap(char[] array, int a, int b) {
- if(a!=b){
- array[a] = (char) (array[a]^array[b]);
- array[b] = (char) (array[b]^array[a]);
- array[a] = (char) (array[b]^array[a]);
- }
- }
- }