Java permutations -
i trying run code prints cyclic permutations, though can first 1 @ moment. runs correctly point have marked can't see going wrong. think has no break in while loop, i'm not sure. here.
package permutation; public class permutation { static int default = 100; public static void main(string[] args) { int n = default; if (args.length > 0) n = integer.parseint(args[0]); int[] oa = new int[n]; (int = 0; < n; i++) oa[i] = + 1; system.out.println("the original array is:"); (int = 0; < oa.length; i++) system.out.print(oa[i] + " "); system.out.println(); system.out.println("a permutation of original array is:"); oa = generaterandompermutation(n); printarray(oa); printpemutation(oa); } static int[] generaterandompermutation(int n)// (a) { int[] = new int[n]; (int = 0; < n; i++) a[i] = + 1; (int = 0; < n; i++) { int r = (int) (math.random() * (n)); int swap = a[r]; a[r] = a[i]; a[i] = swap; } return a; } static void printarray(int a[]) { (int = 0; < a.length; i++) system.out.print(a[i] + " "); system.out.println(); } static void printpemutation(int p[])// (b) { system.out .println("the permutation represented cyclic notation:"); int[] b = new int[p.length]; int m = 0; while (m < p.length)// point @ code screws { if (!check(b, m)) { b = parenthesis(p, m); printparenthesis(b); m++; } else m++; }// if not there repeat } static int[] parenthesis(int p[], int i) { int[] b = new int[p.length]; (int = p[i], j = 0; != b[0]; = p[a - 1], j++) { b[j] = a; } return b; } static void printparenthesis(int b[]) { system.out.print("( "); (int = 0; < b.length && b[i] != 0; i++) system.out.print(b[i] + " "); system.out.print(")"); } static boolean check(int b[], int m) { int = 0; boolean = false; while (i < b.length || !a) { if ((ispresent(m, b, i))){ = true; break; } else i++; } return a; } static boolean ispresent(int m, int b[], int i) { return m == b[i] && m < b.length; } }
among others should check p[m] in check(b, p[m]) instead of m:
in static void printpemutation(int p[]):
while (m < p.length){ if (!check(b, p[m])) { b = parenthesis(p, m); printparenthesis(b); } m++; } then
static boolean check(int b[], int m) { int = 0; while (i < b.length) { if (m == b[i]) { return true; } i++; } return false; } this somehow more want, not fear...
Comments
Post a Comment