" There are 10 types of people those who understand binary and those who don't . "

Saturday, March 27, 2010


WROTE A CODE TO GENERATE ALL POSSIBLE PERMUTATIONS OF A SET OF 9 ELEMENTS
HAVE A LOOK
import java.io.*;
import java.util.*;
class gpint{
public static void main(String args[])throws Exception
{
int i=0,min=0,max=0,j=0,k=0;
String s="",mi="",ma="",t="",t1="";
boolean bo = true, bo2 = false;
System.out.println("enter the elements of set for generating the permutations (upto 9 elements)");
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
s=b.readLine();
if (s.equals(""))
{
System.out.println("no elements inserted");
System.exit(0);
}
System.out.println("****************************************************************************");
int a[]=new int[s.length()];
char b1[]=s.toCharArray();
char b2[]=new char[b1.length];
for (i = 0; i <>
b2[i] = ("" + (i + 1)).charAt(0);
Arrays.sort(b1);
for(i=0;i
mi=mi+1;
ma=ma+s.length();
}
min=Integer.parseInt(mi);
max=Integer.parseInt(ma);
for(i=min;i<=max;i++)
{bo=true;
t=""+i;
t1="";
char b3[]=t.toCharArray();
for(j=0;j
{bo2=false;
for(k=0;k
if(b3[j]==b2[k])
{
bo2=true;
break;
}
if(!bo2)
bo=false;
}
if(!bo)
continue;
else{
for(j=0;j
System.out.print(b1[Integer.parseInt(""+t.charAt(j))-1]);
System.out.println();
}
}
}}
other solution generating permutations without repeated elements
import java.io.*;
import java.util.*;
class gpint2{
public static char a[]=null;
public static void main(String args[])throws Exception{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the elements of set whose permutations are to be generated");
a = (b.readLine()).toCharArray();
Arrays.sort(a);
System.out.println("************GENERATED PERMUTATIONS*****************");
perm(0);
}
public static void perm(int i)
{char t='*';
int j = 0;
for (int n = i; n <>
{
if (i == n)
print();
else
{
for (j = i+1; j <>
{ t = a[i];
a[i]=a[j];
a[j] = t;
perm(i + 1);
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
public static void print()
{
for (int i = 0; i <>
System.out.print(a[i]);
System.out.println();
}
}