数字的全排列常见的是递归的方法。

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.  
  5.  
  6.  
  7. int n = 0;    
  8. void output(int str[], int begin,int N)    
  9. {    
  10.     if (begin == N - 1)    
  11.     {    
  12.         for (int i = 0; i < N; i++)    
  13.         {    
  14.             cout<<str[i]<<"\t";    
  15.         }    
  16.         cout<<endl;    
  17.         n++;    
  18.         return;    
  19.     }    
  20.     for (int p = begin; p < N; p++)    
  21.     {    
  22.         int t = str[p];    
  23.         str[p] = str[begin];    
  24.         str[begin] = t;    
  25.         output(str, begin+1,N);    
  26.         t = str[p];    
  27.         str[p] = str[begin];    
  28.         str[begin] = t;    
  29.     }    
  30. }    
  31.  
  32. int _tmain(int argc, _TCHAR* argv[])    
  33. {    
  34.     int a[5],i;    
  35.     for (i = 0; i < 5; i++)    
  36.     {    
  37.         a[i] = i+1;    
  38.     }    
  39.     cout<<"所有的排序:"<<endl;    
  40.     output(a, 0,5);    
  41.     cout<<endl<<"共有 "<<n<<"种"<<endl;    
  42.     system("pause");    
  43.     return 0;    
  44. }  

如果不用递归,可以有以下方法:

非递归全排列算法的基本思想是:
    1.找到所有排列中最小的一个排列P.
    2.找到刚刚好比P大比其它都小的排列Q,
    3.循环执行第二步,直到找到一个最大的排列,算法结束.
下面用数学的方法描述:
给定已知序列 P =  A1A2A3An ( Ai!=Aj , (1<=i<=n  , 1<=j<=n, i != j  ) )
找到P的一个最小排列Pmin = P1P2P3Pn  有  Pi > P(i-1) (1 < i <= n)
从Pmin开始,总是目前得到的最大的排列为输入,得到下一个排列.
方法为:
1.从低位到高位(从后向前),找出“不符合趋势”的数字。即找到一个Pi,使Pi < P(i+1)。
  若找不到这样的pi,说明我们已经找到最后一个全排列,可以返回了。
2.在 P(i+1)P(i+2)Pn 中,找到一个Pj,便得 Pj"刚刚好大于"Pi. 
  ("刚刚好大于"的意思是:在 P(i+1)P(i+2)Pn 中所有大于Pi的元素构成的集合中最小的元素.)其实从后向前找,第一个大于Pi的即是
3.交换 Pi , Pj 的位置.注意:此处不改变i和j的值,改变的是Pi和Pj.
4.交换后, P1P2P3Pn  并不是准确的后一个排列。因为根据第1步的查找,我们有P(i+1) > P(i+2) > . > Pn
  即使进行了Pi和Pj的交换,这仍然是这一部分最大的一个排列。将此排列逆序倒置(变成最小的排列)即为所求的下一个排列.
5.重复步骤1-4,直到步骤1中找不到“不符合趋势”的数字.

 这个所谓的大小可以这么理解:

1,2,3,4         3,2,1             ((3 * (3) + 2) * (2) + 1) * (1) = 23
1,2,4,3         3,2,0             ((3 * (3) + 2) * (2) + 0) * (1) = 22
1,3,2,4         3,1,1             ((3 * (3) + 1) * (2) + 1) * (1) = 21
1,3,4,2         3,1,0             ((3 * (3) + 1) * (2) + 0) * (1) = 20
1,4,3,2         3,0,1             ((3 * (3) + 0) * (2) + 1) * (1) = 19
.上面的中间转换指的是:每一个数字后面比当前位数字大的数字的个数。                   .                      .

  1. #include "stdafx.h"    
  2. #include <stdio.h>    
  3. #include <stdlib.h>    
  4. #include <string.h>    
  5. #include <iostream>    
  6. using namespace std;    
  7. //交换数组a中下标为i和j的两个元素的值    
  8. void swap(int* a,int i,int j)    
  9. {    
  10.     a[i]^=a[j];    
  11.     a[j]^=a[i];    
  12.     a[i]^=a[j];    
  13. }    
  14. //将数组a中的下标i到下标j之间的所有元素逆序倒置    
  15.  
  16. void reverse(int a[],int i,int j)    
  17. {    
  18.     while (i < j)    
  19.         swap(a,i++,j--);    
  20. }    
  21. void print(int a[],int length)    
  22. {    
  23.     for(int i=0;i<length;++i)    
  24.         cout<<a[i]<<" ";    
  25.     cout<<endl;    
  26. }    
  27. //求取全排列,打印结果    
  28. void combination(int a[],int length)    
  29. {    
  30.     if(length<2) return;    
  31.     while(true)    
  32.     {    
  33.         print(a,length);    
  34.         int i,j;     
  35.         for(i=length-2;i>=0;--i) //找升序的相邻2数,前一个数即替换数     
  36.         {    
  37.             if(a[i]<a[i+1]) break;    
  38.             else if(i==0) return;    
  39.         }    
  40.         for(j=length-1;j>i;--j) //从后往前找到替换点后第一个比替换数大的数    
  41.             if(a[j]>a[i]) break;    
  42.         swap(a,i,j);     
  43.         reverse(a,i+1,length-1); //将替换点以后的序列反转    
  44.     }    
  45. }    
  46. int QsortCmp(const void *pa, const void *pb)      
  47. {      
  48.     return *(char*)pa - *(char*)pb;      
  49. }    
  50. int main()    
  51. {    
  52.     int arr[5] = {1,2,3,4,5};    
  53.     qsort(arr, 5, sizeof(arr[0]), QsortCmp); //排序之前先将数组递增排序    
  54.     combination(arr, 5);    
  55. }   

另外更简单的方法是可以用next_permutation函数,要包含头文件algorithm,但要求数组是排好序的

  1. #include "stdafx.h"    
  2. #include <stdio.h>    
  3. #include <stdlib.h>    
  4. #include <string.h>    
  5. #include <iostream>    
  6. #include <algorithm>  
  7. using namespace std;    
  8.    
  9. int main()    
  10. {    
  11.     int a[] = {1,2,3};  
  12.     do{  
  13.         cout << a[0] << " " << a[1] << " " << a[2] << endl;  
  14.     }  
  15.     while (next_permutation(a,a+3));  
  16.     return 0;  
  17. }