Wednesday, October 4, 2017

Problem Array Sort Shuffle

Code:

public class Akhil {

    public static void main(String [] nt){

        int[] a={1,3,4,2};

        if(!sorted(a)){
            for (int i=0;i<a.length-2;i++){
                if(!sorteda(a[i],a[i+1],a[i+2])){
                    if(sorteda(a[i+1],a[i+2],a[i])){
                        int temp=a[i];
                        a[i]=a[i+1];
                        a[i+1]=a[i+2];
                        a[i+2]=temp;
                        continue;
                    }else {
                        if(sorteda(a[i+2],a[i],a[i+1])){
                            int temp=a[i];
                            a[i]=a[i+2];
                            a[i+2]=a[i+1];
                            a[i+1]=temp;
                            continue;
                        }else{
                            break;
                        }
                    }
                }
            }
        }

        if(sorted(a)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }

    }

    static boolean sorted(int[] a){

        for(int i=0;i<a.length-1;i++){
            if(a[i]>a[i+1]){
                return false;
            }
        }

        return true;
    }

    static boolean sorteda(int a,int b,int c){
        if(a<b && b<c){
            return true;
        }
        return false;
    }

}