title: Algorithm to print all permutations with repetition of numbers
body: <p>I have successfully designed the algorithm to print all the permutations with the repetition of numbers. But the algorithm which I have designed has a flaw. It works only if the chars of the string are unique.</p>
  
  <p>Can someone help me out in extending the algorithm for the case where chars of the string may not be unique..
  My code so far :</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include&lt;cstdio&gt;
  #include&lt;cstdlib&gt;
  #include&lt;cstring&gt;
  #include&lt;climits&gt;
  #include&lt;iostream&gt;
  
  using namespace std;
  
  void _perm(char *arr, char*result, int index)
  {
      static int count = 1;
      if (index == strlen(arr))
      {
          cout &lt;&lt; count++ &lt;&lt; ". " &lt;&lt; result &lt;&lt; endl;
          return;
      }
      for (int i = 0; i &lt; strlen(arr); i++)
      {
          result[index] = arr[i];
          _perm(arr, result, index + 1);
      }
  }
  
  int compare(const void *a, const void *b)
  {
      return (*(char*)a - *(char*)b);
  }
  
  
  
  void perm(char *arr)
  {
      int n = strlen(arr);
      if (n == 0)
          return;
      qsort(arr, n, sizeof(char), compare);
      char *data = new char[n];
      _perm(arr, data, 0);
      free(data);
      return;
  }
  
  
  
  int main()
  {
      char arr[] = "BACD";
      perm(arr);
      return 0;
  }
  </code></pre>
  
  <p>I am printing the output strings in lexicographically sorted way.</p>
  
  <p>I am referring to the example.3 from this page.</p>
  
  <p><a href="http://www.vitutor.com/statistics/combinatorics/permutations_repetition.html" rel="nofollow">http://www.vitutor.com/statistics/combinatorics/permutations_repetition.html</a></p>
  
  <p>Thanks.</p>