Write a Java program to find whether an array is sorted or not.

Suman Barik
0

 public class MyClass {

    public static void main(String args[]) {

      // Practice Problem 6

        boolean isSorted = true;

        int [] arr = {1, 12, 3, 4, 5, 34, 67};

        for(int i=0;i<arr.length-1;i++){

            if(arr[i] > arr[i+1]){

                isSorted = false;

                break;

            }

        }

        if(isSorted){

            System.out.println("The Array is sorted");

        }

        else{

            System.out.println("The Array is not sorted");

        }

    }

}


//output - 

// The Array is not sorted


Post a Comment

0Comments
Post a Comment (0)