遞歸分治求全排列

/**
 * 類名:Test6.java
 * 說明:全排列
 */


public class Test6 {
public static <Type> void perm(Type[] a){
perm(a,0,a.length-1);
}
private static <Type>void perm(Type[]a, int low,int high){
if(low == high){
for(int i = 0; i<=high;i++)
System.out.print(a[i]);
System.out.println();
}else{
for(int i = low;i<=high;i++){
Type temp = a[low];//和後面的交換
a[low] = a[i];
a[i] = temp;
perm(a,low+1,high);
Type temp2 = a[low];//換回來
a[low] = a[i];
a[i] = temp2;
}
}
}
public static void main(String[] args) {
Character[] c = new Character[]{'a','b','c'};
perm(c);

}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章