Type-1: 

Here's a Java program to check if an array of integers is sorted in ascending order, along with an explanation of each step:


public class CheckAscendingOrder
public static void main(String[] args) {
// Step 1: Define an integer array with some elements 
int[] numbers = { 5, 10, 20, 30, 40 }; 
// Step 2: Call the function to check if the array is sorted in ascending order
boolean isAscending = isSortedAscending(numbers); 
// Step 3: Print the result
if (isAscending) { 
 System.out.println("The array is sorted in ascending order.");
 } else
 System.out.println("The array is not sorted in ascending order.");
 } 
 } 
// Function to check if the array is sorted in ascending order 
public static boolean isSortedAscending(int[] arr) {
// Step 4: Check if each element is less than or equal to the next element 
for (int i = 0; i < arr.length - 1; i++) { 
if (arr[i] > arr[i + 1]) { 
return false
// If not in ascending order, return false
 } 
return true
// If the loop completes, the array is sorted in ascending order }
 }

Explanation of each step:

  1. We start by defining an integer array named numbers. In this example, we have an array with elements {5, 10, 20, 30, 40}. You can modify this array to have any set of integer elements.
  2. We call the function isSortedAscending(numbers) to check if the array numbers is sorted in ascending order. The function returns a boolean value (true if the array is sorted in ascending order, false otherwise).
  3. Based on the result obtained from the function call, we print a message indicating whether the array is sorted in ascending order or not.
  4. The function isSortedAscending takes an integer array as input and checks if each element in the array is less than or equal to the next element. It does this by using a traditional for loop that iterates through the array. If at any point, the current element is greater than the next element, it means the array is not in ascending order, and the function returns false.
  5. If the loop completes without encountering any out-of-order elements, it means the array is sorted in ascending order, and the function returns true

When you run this program with the provided array, it will output:

The array is sorted in ascending order.

This is because {5, 10, 20, 30, 40} is sorted in ascending order. If you modify the array elements to be in a different order, the program will output that the array is not sorted in ascending order.

Type-2:

 Another way to check if an array of integers is sorted in ascending order is to compare each element with its succeeding element directly without the need for a separate function.

 Here's the Java program using this approach:


public class CheckAscendingOrder
public static void main(String[] args) {
// Step 1: Define an integer array with some elements 
int[] numbers = { 5, 10, 20, 30, 40 }; 
// Step 2: Check if the array is sorted in ascending order
boolean isAscending = true
for (int i = 0; i < numbers.length - 1; i++) { 
if (numbers[i] > numbers[i + 1]) {
 isAscending = false; break;
 } 
 }
/ Step 3: Print the result
if (isAscending) { 
 System.out.println("The array is sorted in ascending order.");
 } else {
 System.out.println("The array is not sorted in ascending order."); } 
 } 
}

Explanation of the alternative method:

  1. We start by defining an integer array named numbers, just like in the previous example.
  2. We initialize a boolean variable isAscending to true. This variable will be used to keep track of whether the array is sorted in ascending order (true) or not (false).
  3. We use a traditional for loop to iterate through the array, comparing each element with its succeeding element. The loop variable i runs from 0 to numbers.length - 2 because we need to compare each element with its next element.
  4. Inside the loop, we check if the current element (numbers[i]) is greater than the next element (numbers[i + 1]). If this condition is true for any pair of elements, it means the array is not sorted in ascending order, and we set isAscending to false.
  5. If we find any out-of-order elements, we immediately break out of the loop since there's no need to continue further checks.
  6. After the loop has finished, we have the final value of isAscending, indicating whether the array is sorted in ascending order or not.
  7. Finally, we print the result using System.out.println().


When you run this program with the provided array, it will output:

The array is sorted in ascending order.

This is because {5, 10, 20, 30, 40} is sorted in ascending order. If you modify the array elements to be in a different order, the program will output that the array is not sorted in ascending order.