question about permutation problem

Posted by davit-datuashvili on Stack Overflow See other posts from Stack Overflow or by davit-datuashvili
Published on 2010-05-27T11:30:33Z Indexed on 2010/05/27 11:51 UTC
Read the original article Hit count: 178

Filed under:

i have posted similar problem here

http://stackoverflow.com/questions/2920315/permutation-of-array

but i want following we know that with length n there is n! possible permutation from which one such that all element are in order they are in sorted variant so i want break permutation when array is in order and print result but something is wrong i think that problem is repeated of permutation

here is my code

import java.util.*;

public class permut{
public static Random r=new Random();
 public static void display(int a[],int n){

 for (int i=0;i<n;i++){
 System.out.println(a[i]);
}




} 
public static void Permut(int a[],int n){
 int j=0;
int k=0;
while (j<fact(n)){
int   s=r.nextInt(n);
  for (int i=0;i<n;i++){

    k=a[i];
     a[i]=a[s];
a[s]=k;
}

j++;
 if (sorted(a,n))
display(a,n);
   break;


}
}






public static void main(String[]args){

 int a[]=new int[]{3,4,1,2};
int n=a.length;
Permut(a,n);











}


public static int fact(int n){

  if (n==0 || (n==1)  )
  return 1;
 return n*fact(n-1);





}


  public static boolean sorted(int a[],int n  ){

boolean  flag=false;
   for (int i=0;i<n-1;i++){
    if (a[i]<a[i+1]){
     flag=true;
}
   else{
    flag=false;
}

}
 return flag;


}
}

can anybody help me? result is nothing

© Stack Overflow or respective owner

Related posts about algorithm