permutation help

Reply

Join Date: Apr 2008
Posts: 7
Reputation: anifeelings is an unknown quantity at this point 
Solved Threads: 0
anifeelings anifeelings is offline Offline
Newbie Poster

permutation help

 
0
  #1
Apr 18th, 2008
actually i stuck in this prog, not gettting it exactly, i want user to input natural no. of 4 digit and i want its all possible permutations of the number.. plz help me..
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: anifeelings is an unknown quantity at this point 
Solved Threads: 0
anifeelings anifeelings is offline Offline
Newbie Poster

Re: permutation help

 
0
  #2
Apr 18th, 2008
public class Permutations {

// print N! permutation of the characters of the string s (in order)
public static void perm1(String s) { perm1("", s); }
private static void perm1(String prefix, String s) {
int N = s.length();
if (N == 0) System.out.println(prefix);
else {
for (int i = 0; i < N; i++)
perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));
}

}

// print N! permutation of the elements of array a (not in order)
public static void perm2(String s) {
int N = s.length();
char[] a = new char[N];
for (int i = 0; i < N; i++)
a[i] = s.charAt(i);
perm2(a, N);
}

private static void perm2(char[] a, int n) {
if (n == 1) {
System.out.println(a);
return;
}
for (int i = 0; i < n; i++) {
swap(a, i, n-1);
perm2(a, n-1);
swap(a, i, n-1);
}
}

// swap the characters at indices i and j
private static void swap(char[] a, int i, int j) {
char c;
c = a[i]; a[i] = a[j]; a[j] = c;
}



public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String elements = alphabet.substring(0, N);
perm1(elements);
System.out.println();
perm2(elements);
}
}



wilol this work out..?? please do reply..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC