/**
* 求全排列
* @author Administrator
*
*/
public class Perm {
public void perm(int data[],int k,int m) {
if(k==m) {
for(int i=0; i<data.length; i++)
System.out.print(data[i]+" ");
System.out.println("");
}
else {
for(int j=k; j<m; j++) {
swap(data,k,j);
perm(data,k+1,m);
swap(data,k,j);
}
}
}
/**
* 交换一个数组中索引为index1和index2的值
* @param data
* @param index1
* @param index2
*/
void swap(int[] data,int index1,int index2){
int tmp = data[index1];
data[index1] = data[index2];
data[index2] = tmp;
}
public static void main(String[] args) {
Perm p = new Perm();
int data[] = {1,2,3};
p.perm(data, 0, 3);
}
}
* 求全排列
* @author Administrator
*
*/
public class Perm {
public void perm(int data[],int k,int m) {
if(k==m) {
for(int i=0; i<data.length; i++)
System.out.print(data[i]+" ");
System.out.println("");
}
else {
for(int j=k; j<m; j++) {
swap(data,k,j);
perm(data,k+1,m);
swap(data,k,j);
}
}
}
/**
* 交换一个数组中索引为index1和index2的值
* @param data
* @param index1
* @param index2
*/
void swap(int[] data,int index1,int index2){
int tmp = data[index1];
data[index1] = data[index2];
data[index2] = tmp;
}
public static void main(String[] args) {
Perm p = new Perm();
int data[] = {1,2,3};
p.perm(data, 0, 3);
}
}