title: How to permute an array of integers without generating mirrored permutations (Java)
body: <p>I have written a method to permute an array of integers in java. However I could not figure out how I can prevent finding mirrored permutations.
  For example I want all permutations of [1,2,3]. These would be:</p>
  
  <pre><code>[1,2,3]
  [1,3,2]
  [3,1,2]
  
  [3,2,1]
  [2,3,1]
  [2,1,3]
  </code></pre>
  
  <p>The three last permutations are mirrored permutations, that I do not want to find at all. The reason for this is, that I am trying to implement a brute force solution for the traveling salesman problem. By discarding the mirrored permutations I could save some time, as it does not matter in which direction the tour is performed. The cost of the tour would be the same.</p>
  
  <p>My idea was following: As seen in the example above, in the mirrored permutations the 2 is always in front of the 1. If I iterate through the array and find the 2 but not yet the 1, it means that the 1 is saved at a later index. Which means I can stop this permute. However I can not figure out how to perform this check. Moreover I do not know if this approach is the best way to solve this problem.
  How can I perform this check? Are there better approaches?</p>
  
  <pre><code>public void bruteforce(Graph g) {
      int[] elements = new int[g.size()];
      int length = g.size();
  
      for(int i = 0; i &lt; elements.length; i++) {
          elements[i] = i;
      }
  
      permute(elements, length);
  
  }
  
  public void permute(int[] elements, int length) {
      if(length == 1) {
          for(int i = 0; i &lt; length; i++) {
              System.out.println(Arrays.toString(elements));
          }
      }else {
          for(int i = 1; i &lt; length; i++) {
              permute(elements, length-1);
  
              if(length % 2 == 1) {
                  swap(elements, 1, length-1);
              }else {
                  swap(elements, i, length-1);
              }
          }
      }
  }
  
  public void swap(int[] elements, int firstElement, int secondElement) {
      int tmp = elements[firstElement];
      elements[firstElement] = elements[secondElement];
      elements[secondElement] = tmp;
  }
  </code></pre>