# Permutation using recursion

• 03-15-2012
Richieh
Permutation using recursion
Hello all. I'm supposed to store a set of permutations (type list<int>) inside a set of list<int>'s. I can't figure out how to use recursion inside my permutate method. Can anyone help? I'm completely lost and this is due Monday.

Code:

``` #include <iostream> #include <algorithm> #include <list> #include <string> #include <cctype> #include<set> using namespace std; class perm{    public:    void permutate();  void setList(int size);  void display();      private:  int max;  list<int> numbers;  set<list<int> > permutations;                      }; void perm::setList(int size) {   for (int i = 1; i <= size; i++)   {       numbers.push_back(i);   }   }   void perm :: permutate(list<int> numbers) {             if (numbers.size() == 1)     {         permutations.insert(numbers);     }     else{               list<int>::iterator it;     for (it = numbers.begin(); it != numbers.end(); it++)     {         int c;         c = numbers.front();         numbers.pop_front();         numbers.front() = c;                 set<list<int> > subPerms;         subPerms = permutate(                         /*         for (it2 = subPerms.begin(); it2 != subPerms.end(); it2++)         {             subPerms.insert(it2,numbers);             numbers.pop_front();;                 }         */                   }         }         }               void perm :: display() {     set<list<int> >::iterator it;     for (it = permutations.begin(); it != permutations.end(); it++)     {         list<int>::iterator j;         cout << "( ";         list<int> temp = *it;         for (j = temp.begin(); j != temp.end(); j++) {             cout << *j << " ";         }         cout << ")";         cout << endl;     } } int main() {         perm test;     test.setList(2);     test.permutate();     test.display();                             system("pause");     return 0; }```
• 03-15-2012
whiteflags
I think this method lends itself well to recursion:
Permutation - Wikipedia, the free encyclopedia

Step one can be used as the base case for the recursion. Until the algorithm is done, you can just have your function call itself.