Advertisement
steverobinson

Steve Robinson | Permutation Generator | C++

Dec 22nd, 2010
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. //Generates permutations of combinations of elements
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void perm(int a[],int k,int n)
  8. {
  9.     if(k==n)
  10.     {
  11.         for(int i=1;i<=n;i++)
  12.             cout<<a[i]<<" ";
  13.         cout<<endl;
  14.     }
  15.     else
  16.     {
  17.         for(int i=k;i<=n;i++)
  18.         {
  19.             int t=a[k];
  20.             a[k]=a[i];
  21.             a[i]=t;
  22.             perm(a,k+1,n);
  23.             t=a[k];
  24.             a[k]=a[i];
  25.             a[i]=t;
  26.  
  27.         }
  28.     }
  29. }
  30.  
  31. int main()
  32. {
  33.     int a[4]={0,1,2,3};  //use your own type and values
  34.     perm(a,1,3);     //k=1 always at start
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement