Results 1 to 5 of 5
Thread: array permutations
- 04-07-2010, 11:24 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
- 04-07-2010, 11:25 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What have you tried?
- 04-07-2010, 11:30 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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)); } }
- 04-07-2010, 01:28 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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
- 04-08-2010, 05:57 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
Permutations
By m00nchile in forum New To JavaReplies: 3Last Post: 03-25-2010, 08:58 PM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
Find all permutations of a number
By matzahboy in forum New To JavaReplies: 6Last Post: 12-02-2008, 03:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks