Skip to main content
Removed line numbers in pasted code as it served no purpose, but is frustrating for anyone copying code.
Source Link
1 // Permute.java -- A class generating all permutations 2  3 import java.util.Iterator; 4 import java.util.NoSuchElementException; 5 import java.lang.reflect.Array; 6  7 public class Permute implements Iterator { 8  9  private final int size; 10  private final Object [] elements; // copy of original 0 .. size-1 11  private final Object ar; // array for output, 0 .. size-1 12  private final int [] permutation; // perm of nums 1..size, perm[0]=0 13  14  private boolean next = true; 15  16  // int[], double[] array won't work :-( 17  public Permute (Object [] e) { 18  size = e.length; 19  elements = new Object [size]; // not suitable for primitives 20  System.arraycopy (e, 0, elements, 0, size); 21  ar = Array.newInstance (e.getClass().getComponentType(), size); 22  System.arraycopy (e, 0, ar, 0, size); 23  permutation = new int [size+1]; 24  for (int i=0; i<size+1; i++) { 25  permutation [i]=i; 26  } 27  } 28  29  private void formNextPermutation () { 30  for (int i=0; i<size; i++) { 31  // i+1 because perm[0] always = 0 32  // perm[]-1 because the numbers 1..size are being permuted 33  Array.set (ar, i, elements[permutation[i+1]-1]); 34  } 35  } 36  37  public boolean hasNext() { 38  return next; 39  } 40  41  public void remove() throws UnsupportedOperationException { 42  throw new UnsupportedOperationException(); 43  } 44  45  private void swap (final int i, final int j) { 46  final int x = permutation[i]; 47  permutation[i] = permutation [j]; 48  permutation[j] = x; 49  } 50  51  // does not throw NoSuchElement; it wraps around! 52  public Object next() throws NoSuchElementException { 53  54  formNextPermutation (); // copy original elements 55  56  int i = size-1; 57  while (permutation[i]>permutation[i+1]) i--; 58  59  if (i==0) { 60  next = false; 61  for (int j=0; j<size+1; j++) { 62  permutation [j]=j; 63  } 64  return ar; 65  } 66  67  int j = size; 68  69  while (permutation[i]>permutation[j]) j--; 70  swap (i,j); 71  int r = size; 72  int s = i+1; 73  while (r>s) { swap(r,s); r--; s++; } 74  75  return ar; 76  } 77  78  public String toString () { 79  final int n = Array.getLength(ar); 80  final StringBuffer sb = new StringBuffer ("["); 81  for (int j=0; j<n; j++) { 82  sb.append (Array.get(ar,j).toString()); 83  if (j<n-1) sb.append (","); 84  } 85  sb.append("]"); 86  return new String (sb); 87  } 88  89  public static void main (String [] args) { 90  for (Iterator i = new Permute(args); i.hasNext(); ) { 91  final String [] a = (String []) i.next(); 92  System.out.println (i); 93  } 94  } 95 } 
1 // Permute.java -- A class generating all permutations 2  3 import java.util.Iterator; 4 import java.util.NoSuchElementException; 5 import java.lang.reflect.Array; 6  7 public class Permute implements Iterator { 8  9  private final int size; 10  private final Object [] elements; // copy of original 0 .. size-1 11  private final Object ar; // array for output, 0 .. size-1 12  private final int [] permutation; // perm of nums 1..size, perm[0]=0 13  14  private boolean next = true; 15  16  // int[], double[] array won't work :-( 17  public Permute (Object [] e) { 18  size = e.length; 19  elements = new Object [size]; // not suitable for primitives 20  System.arraycopy (e, 0, elements, 0, size); 21  ar = Array.newInstance (e.getClass().getComponentType(), size); 22  System.arraycopy (e, 0, ar, 0, size); 23  permutation = new int [size+1]; 24  for (int i=0; i<size+1; i++) { 25  permutation [i]=i; 26  } 27  } 28  29  private void formNextPermutation () { 30  for (int i=0; i<size; i++) { 31  // i+1 because perm[0] always = 0 32  // perm[]-1 because the numbers 1..size are being permuted 33  Array.set (ar, i, elements[permutation[i+1]-1]); 34  } 35  } 36  37  public boolean hasNext() { 38  return next; 39  } 40  41  public void remove() throws UnsupportedOperationException { 42  throw new UnsupportedOperationException(); 43  } 44  45  private void swap (final int i, final int j) { 46  final int x = permutation[i]; 47  permutation[i] = permutation [j]; 48  permutation[j] = x; 49  } 50  51  // does not throw NoSuchElement; it wraps around! 52  public Object next() throws NoSuchElementException { 53  54  formNextPermutation (); // copy original elements 55  56  int i = size-1; 57  while (permutation[i]>permutation[i+1]) i--; 58  59  if (i==0) { 60  next = false; 61  for (int j=0; j<size+1; j++) { 62  permutation [j]=j; 63  } 64  return ar; 65  } 66  67  int j = size; 68  69  while (permutation[i]>permutation[j]) j--; 70  swap (i,j); 71  int r = size; 72  int s = i+1; 73  while (r>s) { swap(r,s); r--; s++; } 74  75  return ar; 76  } 77  78  public String toString () { 79  final int n = Array.getLength(ar); 80  final StringBuffer sb = new StringBuffer ("["); 81  for (int j=0; j<n; j++) { 82  sb.append (Array.get(ar,j).toString()); 83  if (j<n-1) sb.append (","); 84  } 85  sb.append("]"); 86  return new String (sb); 87  } 88  89  public static void main (String [] args) { 90  for (Iterator i = new Permute(args); i.hasNext(); ) { 91  final String [] a = (String []) i.next(); 92  System.out.println (i); 93  } 94  } 95 } 
// Permute.java -- A class generating all permutations import java.util.Iterator; import java.util.NoSuchElementException; import java.lang.reflect.Array; public class Permute implements Iterator { private final int size; private final Object [] elements; // copy of original 0 .. size-1 private final Object ar; // array for output, 0 .. size-1 private final int [] permutation; // perm of nums 1..size, perm[0]=0 private boolean next = true; // int[], double[] array won't work :-( public Permute (Object [] e) { size = e.length; elements = new Object [size]; // not suitable for primitives System.arraycopy (e, 0, elements, 0, size); ar = Array.newInstance (e.getClass().getComponentType(), size); System.arraycopy (e, 0, ar, 0, size); permutation = new int [size+1]; for (int i=0; i<size+1; i++) { permutation [i]=i; } } private void formNextPermutation () { for (int i=0; i<size; i++) { // i+1 because perm[0] always = 0 // perm[]-1 because the numbers 1..size are being permuted Array.set (ar, i, elements[permutation[i+1]-1]); } } public boolean hasNext() { return next; } public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } private void swap (final int i, final int j) { final int x = permutation[i]; permutation[i] = permutation [j]; permutation[j] = x; } // does not throw NoSuchElement; it wraps around! public Object next() throws NoSuchElementException { formNextPermutation (); // copy original elements int i = size-1; while (permutation[i]>permutation[i+1]) i--; if (i==0) { next = false; for (int j=0; j<size+1; j++) { permutation [j]=j; } return ar; } int j = size; while (permutation[i]>permutation[j]) j--; swap (i,j); int r = size; int s = i+1; while (r>s) { swap(r,s); r--; s++; } return ar; } public String toString () { final int n = Array.getLength(ar); final StringBuffer sb = new StringBuffer ("["); for (int j=0; j<n; j++) { sb.append (Array.get(ar,j).toString()); if (j<n-1) sb.append (","); } sb.append("]"); return new String (sb); } public static void main (String [] args) { for (Iterator i = new Permute(args); i.hasNext(); ) { final String [] a = (String []) i.next(); System.out.println (i); } } } 
Pasted in code from link to prevent this answer becoming irrelevant if page is taken down.
Source Link

Edit: code pasted below to protect against link-death:

1 // Permute.java -- A class generating all permutations 2 3 import java.util.Iterator; 4 import java.util.NoSuchElementException; 5 import java.lang.reflect.Array; 6 7 public class Permute implements Iterator { 8 9 private final int size; 10 private final Object [] elements; // copy of original 0 .. size-1 11 private final Object ar; // array for output, 0 .. size-1 12 private final int [] permutation; // perm of nums 1..size, perm[0]=0 13 14 private boolean next = true; 15 16 // int[], double[] array won't work :-( 17 public Permute (Object [] e) { 18 size = e.length; 19 elements = new Object [size]; // not suitable for primitives 20 System.arraycopy (e, 0, elements, 0, size); 21 ar = Array.newInstance (e.getClass().getComponentType(), size); 22 System.arraycopy (e, 0, ar, 0, size); 23 permutation = new int [size+1]; 24 for (int i=0; i<size+1; i++) { 25 permutation [i]=i; 26 } 27 } 28 29 private void formNextPermutation () { 30 for (int i=0; i<size; i++) { 31 // i+1 because perm[0] always = 0 32 // perm[]-1 because the numbers 1..size are being permuted 33 Array.set (ar, i, elements[permutation[i+1]-1]); 34 } 35 } 36 37 public boolean hasNext() { 38 return next; 39 } 40 41 public void remove() throws UnsupportedOperationException { 42 throw new UnsupportedOperationException(); 43 } 44 45 private void swap (final int i, final int j) { 46 final int x = permutation[i]; 47 permutation[i] = permutation [j]; 48 permutation[j] = x; 49 } 50 51 // does not throw NoSuchElement; it wraps around! 52 public Object next() throws NoSuchElementException { 53 54 formNextPermutation (); // copy original elements 55 56 int i = size-1; 57 while (permutation[i]>permutation[i+1]) i--; 58 59 if (i==0) { 60 next = false; 61 for (int j=0; j<size+1; j++) { 62 permutation [j]=j; 63 } 64 return ar; 65 } 66 67 int j = size; 68 69 while (permutation[i]>permutation[j]) j--; 70 swap (i,j); 71 int r = size; 72 int s = i+1; 73 while (r>s) { swap(r,s); r--; s++; } 74 75 return ar; 76 } 77 78 public String toString () { 79 final int n = Array.getLength(ar); 80 final StringBuffer sb = new StringBuffer ("["); 81 for (int j=0; j<n; j++) { 82 sb.append (Array.get(ar,j).toString()); 83 if (j<n-1) sb.append (","); 84 } 85 sb.append("]"); 86 return new String (sb); 87 } 88 89 public static void main (String [] args) { 90 for (Iterator i = new Permute(args); i.hasNext(); ) { 91 final String [] a = (String []) i.next(); 92 System.out.println (i); 93 } 94 } 95 } 

Edit: code pasted below to protect against link-death:

1 // Permute.java -- A class generating all permutations 2 3 import java.util.Iterator; 4 import java.util.NoSuchElementException; 5 import java.lang.reflect.Array; 6 7 public class Permute implements Iterator { 8 9 private final int size; 10 private final Object [] elements; // copy of original 0 .. size-1 11 private final Object ar; // array for output, 0 .. size-1 12 private final int [] permutation; // perm of nums 1..size, perm[0]=0 13 14 private boolean next = true; 15 16 // int[], double[] array won't work :-( 17 public Permute (Object [] e) { 18 size = e.length; 19 elements = new Object [size]; // not suitable for primitives 20 System.arraycopy (e, 0, elements, 0, size); 21 ar = Array.newInstance (e.getClass().getComponentType(), size); 22 System.arraycopy (e, 0, ar, 0, size); 23 permutation = new int [size+1]; 24 for (int i=0; i<size+1; i++) { 25 permutation [i]=i; 26 } 27 } 28 29 private void formNextPermutation () { 30 for (int i=0; i<size; i++) { 31 // i+1 because perm[0] always = 0 32 // perm[]-1 because the numbers 1..size are being permuted 33 Array.set (ar, i, elements[permutation[i+1]-1]); 34 } 35 } 36 37 public boolean hasNext() { 38 return next; 39 } 40 41 public void remove() throws UnsupportedOperationException { 42 throw new UnsupportedOperationException(); 43 } 44 45 private void swap (final int i, final int j) { 46 final int x = permutation[i]; 47 permutation[i] = permutation [j]; 48 permutation[j] = x; 49 } 50 51 // does not throw NoSuchElement; it wraps around! 52 public Object next() throws NoSuchElementException { 53 54 formNextPermutation (); // copy original elements 55 56 int i = size-1; 57 while (permutation[i]>permutation[i+1]) i--; 58 59 if (i==0) { 60 next = false; 61 for (int j=0; j<size+1; j++) { 62 permutation [j]=j; 63 } 64 return ar; 65 } 66 67 int j = size; 68 69 while (permutation[i]>permutation[j]) j--; 70 swap (i,j); 71 int r = size; 72 int s = i+1; 73 while (r>s) { swap(r,s); r--; s++; } 74 75 return ar; 76 } 77 78 public String toString () { 79 final int n = Array.getLength(ar); 80 final StringBuffer sb = new StringBuffer ("["); 81 for (int j=0; j<n; j++) { 82 sb.append (Array.get(ar,j).toString()); 83 if (j<n-1) sb.append (","); 84 } 85 sb.append("]"); 86 return new String (sb); 87 } 88 89 public static void main (String [] args) { 90 for (Iterator i = new Permute(args); i.hasNext(); ) { 91 final String [] a = (String []) i.next(); 92 System.out.println (i); 93 } 94 } 95 } 
Source Link
Mr.Expert
  • 466
  • 2
  • 3

Here is an implementation of the Permutation in Java:

Permutation - Java

You should have a check on it!