Results 1 to 5 of 5
  1. #1
    mtK
    mtK is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Exclamation array permutations

    Hello.
    How can I get all permutations in array[]. For example I have:
    int[] array = {1, 2, 3};

    As result I need:
    123
    132
    213
    231
    312
    321

    Can you help me with that? Thanks in advance.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What have you tried?

  3. #3
    mtK
    mtK is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    This works perfectly with Strings, but I can't and can't get it work with an array. :/

    Java Code:
    public  static void permutacija(String s) { permutacija("", s); }
    private static void permutacija(String prefix, String s) {
         int N = s.length();
         if (N == 0) {
              System.out.println(prefix);
         } else {
              for (int i = 0; i < N; i++)
                   permutacija(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));
         }
    }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    There is a nice iterative algorithm that finds a next permutation given a current one; it finds permutations in lexicographic order (dictionary order), e.g. given the permutation 2, 3, 1 it finds the permutation 3, 1, 2. If the current permutation is 3, 2, 1 there is no next permutation; here's the algoritm:

    1) find the largest i such that a[i] < a[i+1]
    2) if no such i exists there is no next permutation.
    3) find the largest j > i such that a[j] > a[i]; such j always exists
    4) swap a[i] and a[j]
    5) reverse the elements a[i+1], a[i+2] ... a[n] (all elements to the right of i).

    I'm sure you can construct a bit of Java from this pseudo code.

    kind regards,

    Jos

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Assuming the size of the array is not fixed (if it is, you could just use nested loops), the best way is probably a re-entrant method.

Similar Threads

  1. Permutations
    By m00nchile in forum New To Java
    Replies: 3
    Last Post: 03-25-2010, 08:58 PM
  2. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  3. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Find all permutations of a number
    By matzahboy in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 03:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •