title: How to get an array all permutation in Actionscript 3.0?
body: <p>Suppose I have an array A = [0,1,2]. How do I get the 2 dimensional array perm_A = [[0,1,2],[0,2,1][1,0,2][1,2,0][2,1,0],[2,0,1]? The order of which the permutation appper in perm_A does not matter. </p>
  
  <p>I used the code in <a href="https://en.wikipedia.org/wiki/Heap%27s_algorithm" rel="nofollow noreferrer">Heap's Algoritm</a>. </p>
  
  <pre><code>function generatePermute(k:int, arr:Array){
          if (k == 1){
          perm_list.push(arr);
      }
      else {
          generatePermute(k-1,arr);
      }
      for (var i = 0; i &lt; k-1; i++){
          if (k % 2 == 0){
              swap_arr(arr, i, k-1);
          }
          else{
              swap_arr(arr, 0, k-1);
          }
          generatePermute(k-1,arr);
      }
  }
  
  function swap_arr(input, index_A, index_B) {
      var temp = input[index_A];
   
      input[index_A] = input[index_B];
      input[index_B] = temp;
  }
  
  var arr:Array = [0,1,2, 3]
  var perm_list:Array = new Array();
  generatePermute(4,arr);
  for (var i = 0; i&lt;perm_list.length;i++){
      trace(perm_list[i])
  }
  </code></pre>
  
  <p>The result is :</p>
  
  <pre><code>1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  1,2,3,0
  </code></pre>
  
  <p>which is clearly wrong.</p>
  
  <h2>update</h2>
  
  <p>if I change perm_list.push(arr); to trace (arr), I do get correct result. However, this only ouputs all the permutations in the console. I want to collect all of these permutations into a 2 dimensional array.</p>
  
  <pre><code>function generatePermute(k:int, arr:Array){
      if (k == 1){
          trace(arr)
      }
      else {
          generatePermute(k-1,arr);
      }
      for (var i = 0; i &lt; k-1; i++){
          if (k % 2 == 0){
              swap_arr(arr, i, k-1);
          }
          else{
              swap_arr(arr, 0, k-1);
          }
          generatePermute(k-1,arr);
      }
  }
  
  function swap_arr(input, index_A, index_B) {
      var temp = input[index_A];
   
      input[index_A] = input[index_B];
      input[index_B] = temp;
  }
  
  var arr:Array = [0,1,2, 3]
  var perm_list:Array = new Array();
  generatePermute(4,arr);
  </code></pre>
  
  <p>which outputs</p>
  
  <pre><code>0,1,2,3
  1,0,2,3
  2,0,1,3
  0,2,1,3
  1,2,0,3
  2,1,0,3
  3,1,0,2
  1,3,0,2
  0,3,1,2
  3,0,1,2
  1,0,3,2
  0,1,3,2
  0,2,3,1
  2,0,3,1
  3,0,2,1
  0,3,2,1
  2,3,0,1
  3,2,0,1
  3,2,1,0
  2,3,1,0
  1,3,2,0
  3,1,2,0
  2,1,3,0
  1,2,3,0
  </code></pre>
  
  <p>My guess is it seems there is something wrong with perm_list.push(arr)</p>