算法设计:全排列算法代码实现

在上星期的算法设计课程的学习中,我们学习了两种全排列算法,该算法用于求出数组{1,2,3,...,n}的所有可能的排列,今天我们就来看看这个算法的具体代码实现。

 

1. 第一种算法

第一种算法和我们现实生活中习惯的方法较为相似,以{1,2,3}为例,我们先写出第一种排列123,然后将2与3交换,得到132;再回到123,交换1与2得到213,再将1与3交换.....直到得到所有的排列。

 

该算法伪码如下:

PERMUTATIONS1(int n):

  1. for j←1 to n
  2.   a[j]←j
  3. end for
  4. perm1(1)

perm1(int m):

  1. if m = n then output a[1...n]
  2. else
  3.   for j←m to n
  4.     互换a[j]和a[m]
  5.     perm1(m+1)
  6.     互换a[j]和a[m]
  7.   end for
  8. end if

 

具体代码实现如下:

 

 1 //第一种排列生成算法
 2 public class Permutations1 {
 3     private int a[] = new int[50];    //声明存放数据的数组
 4     private int length = 0;
 5     //构造函数
 6     public Permutations1(int length)
 7     {
 8         this.length = length;
 9         for(int i=1;i<=length;i++)
10             a[i] = i;
11     }
12     
13     //执行全排列算法
14     public void perm1(int n)
15     {
16         if(n == length)
17             this.dispArray();    //到最底层时输出排列结果
18         else 
19         {
20             for(int i=n;i<=length;i++)
21             {
22                 this.swap(i, n);  //交换两数的值
23                 perm1(n + 1);    //递归执行perm1
24                 this.swap(i, n);    //恢复位置
25             }
26         }
27     }
28     
29     //交换数组中两数的值
30     public void swap(int x, int y)
31     {
32         int t = a[x];
33         a[x] = a[y];
34         a[y] = t;
35     }
36     
37     //输出排列
38     public void dispArray()
39     {
40         for(int i=1;i<=length;i++)
41             System.out.print(a[i]);
42         System.out.println();
43     }
44 }

 

 

2. 第二种算法

第二种算法比较类似于我们中学学的排列组合,还是以{1,2,3}为例,我们先确定第一个位置的数字3,再确定第二个位置的数字2,再确定最后一个位置的数字1,然后我们再向前恢复,再次确定第二个位置的数字....直到得到所有的排列。

 

该算法伪码如下:

PERMUTATIONS2(int n):

  1. for j←1 to n
  2.   a[j]←0
  3. end for
  4. perm2(n)

perm2(int m):

  1. if m=0 then output a[1...n]
  2. else
  3.   for j←1 to n
  4.     if a[j]=0 then
  5.       a[j]←m
  6.       perm2(m-1)
  7.       a[j]←0
  8.     end if
  9.   end for
  10. end if

具体代码如下:

 1 //第二种排列生成算法
 2 public class Permutations2 {
 3     private int a[] = new int[50];    //声明存放数据的数组
 4     private int length = 0;    //声明数组长度
 5     
 6     //构造函数
 7     public Permutations2(int length)
 8     {
 9         this.length = length;
10         for(int i = 1;i<=length;i++)
11             a[i] = 0;    //将所有元素记为零
12     }
13     
14     public void perm2(int n)
15     {
16         if(n == 0)
17             this.dispArray();
18         else 
19         {
20             for(int i=1;i<=length;i++)
21             {
22                 if(a[i] == 0)    //如果该位为空
23                     {
24                         a[i] = n;    //将n的值赋给该位
25                         perm2(n - 1);    //递归执行
26                         a[i] = 0;    //恢复
27                     }
28             }
29         }
30     }
31     
32     //输出排列
33     public void dispArray()
34     {
35         for(int i=1;i<=length;i++)
36             System.out.print(a[i]);
37         System.out.println();
38     }
39 }

 

posted @ 2018-10-29 20:24  Sunrise_1018  阅读(10606)  评论(2编辑  收藏  举报