1

I am trying to take in some integers through command line arguments and then generate all the possible ordered sets from the set of numbers. The following code does that but it repeats the ordered sets.

For example, I input "2 3 4" as the command line input, the output is as follows.

 2 3 4
 3 2 4
 4 3 2
 3 2 4
 2 3 4
 2 4 3
 4 3 2
 2 4 3
 2 3 4

I don't want to repeat an ordered set. For this, I tried to convert the integer array to String and print it only if it has not been repeated by using Equals method. So, when I used Array.toString(b), I was expecting to return a integer converted to string but it returned nine addresses.

My questions are:

  1. How do I get a string containing the same values of integers instead of the addresses?
  2. Please help me with the method I've used or, suggest any simpler method so that an ordered set of numbers, once used is not repeated once generated, so that while printing final set of ordered sets, each one of them are unique.

My code is as follows:

class Shuffler
{
    void swapWith(int[] a,int m,int n,int sizeOfArray)
    {
        int[] b = new int[sizeOfArray];
        b = a.clone();
        int temp = b[n];
        b[n] = b[m];
        b[m]=temp;
        for(int k=0;k<sizeOfArray;k++)
            {System.out.print(" "+b[k]);}
        System.out.println("");
    }
    public static void main(String[] args)
    {
        int a[] = new int[10];
        Shuffler SH = new Shuffler();
        for(int i=0;i<args.length;i++)
        {
            a[i] = Integer.parseInt(args[i]);
        }
        System.out.println("Possibilities are..");
        for(int k=0;k<args.length;k++)
        {
        for(int j=0;j<args.length;j++)
        {
            SH.swapWith(a,k,j,args.length);
        }
    }
}
}
vipulnj
  • 135
  • 1
  • 4
  • 9
  • Duplicate of http://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string – Danstahr Jan 05 '14 at 13:31
  • @Danstahr that doesn't eliminate duplicates. – Peter Lawrey Jan 05 '14 at 13:32
  • @PeterLawrey: Of course it does. A sane permutations generator won't yield two permutations that are the same. – Danstahr Jan 05 '14 at 13:34
  • @Danstahr The solutions assume all elements are unique to start with. If there are any duplicates, it will give duplicate results. – Peter Lawrey Jan 05 '14 at 14:05
  • Take a look at guavas [Collection2](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Collections2.html) package, especially the [orderedPermutations(...)](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Collections2.html#orderedPermutations(java.lang.Iterable)) function. – Ortwin Angermeier Jan 05 '14 at 14:40

2 Answers2

1

What you need to do is to implement some known algorithm which generates all permutations of a set. You may for example get familiar with and implement this one.

http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order

So given the String "2 3 4", first split it to get 3 Strings "2", "3", "4". Then just implement some algorithm generating all permutations of a given set.

See also.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Using some classes I implemented a while ago for:

  • UnmodifiableLinkedList - a LinkedList with constant-time addAll() functions (the java.util.LinkedList class implements addAll() in linear-time) which does not support removing elements from the list (although I do have an extension to it which adds in that support); and
  • PermutatableLinkedList which includes an implementation of the Steinhaus-Johnson-Trotter permutation algorithm to generate all possible permutations of the list.

The imported files are all available from https://github.com/MT-0/Permutations.

import com.github.MT_0.permutations.PermutableUnmodifiableLinkList;
import com.github.MT_0.permutations.UnmodifiableLinkList;

public class Answer {
    public static void main(String[] args) {
        final PermutableUnmodifiableLinkList<Integer> permutableList = new PermutableUnmodifiableLinkList<Integer>();
        for ( String str : args )
        {
            permutableList.addTail( Integer.parseInt( str ) );
        }
        final long numPermutations = permutableList.getTotalPermutations();
        for ( long i = 0; i < numPermutations; ++i )
        {
            final UnmodifiableLinkList<Integer> permutation = permutableList.getPermutation();
            for ( Integer element : permutation )
                System.out.print( element + " ");
            System.out.println();
            permutableList.nextPermutation();
        }
    }
}
MT0
  • 143,790
  • 11
  • 59
  • 117