Check if Array is Sorted Java

Check if Array is Sorted Java | In this section, we will check whether the given array is sorted or not, it can be in ascending or maybe in descender order. See the below examples to understand them in detail. Also see:- How to Sort an Array in Java

Example-1 to check if array is sorted Java:
Array = {1, 2, 3, 4, 5}
Yes, given array is sorted.

Example-2 for how to check if an array is sorted:-
Array = {6, 4, 6, 7, 8, 9}
No, the given array is not sorted.

There are no direct built-in functions or methods to find whether the array is sorted or not hence we have defined a function or method of our own, to do this we use recursion or iteration techniques in Java.

Check if Array is Sorted Java Using Iteration

Iteration is done by using loops that iterate over the loop again and again until it satisfies the condition. Now let us see how to check if an array is sorted Java.

import java.util.Arrays;
import java.util.Scanner;

public class Main {

   public static boolean isSortedArray(int array[]) {
      if (array.length == 0 || array.length == 1) {
         return true;
      }

      if (array[0] > array[1]) {
         // descending order
         for (int i = 1; i < array.length; i++) {
            if (array[i - 1] < array[i]) {
               return false;
            }
         }
      } else {
         // ascending order
         for (int i = 1; i < array.length; i++) {
            if (array[i - 1] > array[i]) {
               return false;
            }
         }
      }

      return true;
   }

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter the size of the array: ");
      int n = scan.nextInt();
      int array[] = new int[n];
      System.out.println("Enter array elements: ");
      for (int i = 0; i < array.length; i++) {
         array[i] = scan.nextInt();
      }
      System.out.println("Array = " + Arrays.toString(array));

      if (isSortedArray(array)) {
         System.out.println("Yes, the given array is sorted.");
      } else {
         System.out.println("No, array is not sorted.");
      }
      scan.close();
   }
}

Output:-

Enter the size of the array: 5
Enter array elements:
10 15 20 35 50
Array = [10, 15, 20, 35, 50]
Yes, the given array is sorted.

Enter the size of the array: 5
Enter array elements:
50 45 36 25 9
Array = [50, 45, 36, 25, 9]
Yes, the given array is sorted.

Enter the size of the array: 5
Enter array elements:
30 10 20 15 25
Array = [30, 10, 20, 15, 25]
No, array is not sorted.

Enter the size of the array: 5
Enter array elements:
10 10 20 20 20
Array = [10, 10, 20, 20, 20]
Yes, the given array is sorted.

In the above program to check if array is sorted Java, we have defined isSortedArray() method. In isSortedArray() method, we have checked the number of elements in the given array. If the array contains either 0 elements or 1 element then we can say that array is sorted.

Else we compare the first and second elements of the array. If the second element is smaller than the first element then the array must be in descending order. In that case, every next element must not be greater than the previous element.

Similarly, if the second element is greater than the first element then the array must be in ascending order. In that case, each next element of the array must not be lesser than the previous element else the given array is not sorted.

How to Check if Array is Sorted Using Recursion

import java.util.Arrays;
import java.util.Scanner;

public class Main {

   public static boolean isSortedArray(int[] array) {
      return isSortedInAscending(array, array.length) ||
             isSortedInDescending(array, array.length);
   }

   private static boolean isSortedInAscending(int array[], int n) {
      if (n == 1 || n == 0) {
         return true;
      }
      if (array[n - 1] < array[n - 2]) {
         return false;
      }
      return isSortedInAscending(array, n - 1);
   }

   private static boolean isSortedInDescending(int array[], int n) {
      if (n == 1 || n == 0) {
         return true;
      }
      if (array[n - 1] > array[n - 2]) {
         return false;
      }
      return isSortedInDescending(array, n - 1);
   }

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter the size of the array: ");
      int n = scan.nextInt();
      int array[] = new int[n];
      System.out.println("Enter array elements: ");
      for (int i = 0; i < array.length; i++) {
         array[i] = scan.nextInt();
      }
      System.out.println("Array = " + Arrays.toString(array));

      if (isSortedArray(array)) {
         System.out.println("Yes, the given array is sorted.");
      } else {
         System.out.println("No, array is not sorted.");
      }
      scan.close();
   }
}

Output:-

Enter the size of the array: 5
Enter array elements:
50 45 36 25 9
Array = [50, 45, 36, 25, 9]
Yes, the given array is sorted.

Enter the size of the array: 5
Enter array elements:
10 20 30 40 50
Array = [10, 20, 30, 40, 50]
Yes, the given array is sorted.

Enter the size of the array: 5
Enter array elements:
10 20 10 30 15
Array = [10, 20, 10, 30, 15]
No, array is not sorted.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *