Check array is sorted or not ? in java.

 public class Main

{

public static void main(String[] args) {

      int[] arr={12,11,16,20,35};

      int n=arr.length;

      boolean flag=true;

      for(int i=0; i<=n-2; i++){

            if(arr[i+1]<arr[i])

            {

                  flag=false;

                  break;

            }

      }

      if(flag==false)

      System.out.println("unsorted");

      else

      System.out.println("sorted");

}

}

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.