Check If An Array Is Sorted In Java
In this tutorial, we will see a Java program to check if an array is sorted in Java.
Introduction
We will write a Java program to check if the array is sorted or not.
We will take a sample integer array arr = {1, 3 , 5, 9};
We will return true
if the array is sorted or false
if the array is not sorted.
Check if an array is sorted or not in Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* A Java program to check if the array of integers is sorted
* or not using for loop iteration.
*
* @author coderolls.com
*
*/
public class SortedArrayTester {
public static void main(String[] args) {
int[] arr = {2, 5, 9, 8, 11, 18, 13};
boolean isSorted = isArraySorted(arr);
//Use this to check the sorted case
/*
* int[] arr2 = {2, 5, 9, 11, 18};
* boolean isSorted = isArraySorted(arr2);
*/
if(isSorted) {
System.out.println("The array is sorted.");
}
else {
System.out.println("The array is not sorted.");
}
}
private static boolean isArraySorted(int[] arr) {
int n = arr.length;
for(int i=0; i<n-1; i++) {
//in a sorted array current number should be
//always less than the next number in the array
if(arr[i]>arr[i+1]) {
return false;
}
}
return true;
}
}
Output:
1
The array is not sorted.
Explanation:
- In the
main
method we are taking a sample arrayarr
and passing it to theisArraySorted()
method to check if the array is sorted or not. This method returns aboolean
value.true
- if an array is sorted,false
- if an array is not sorted - In the
isArraySorted()
method, we are iterating over an array from index0
ton-1
, (n
-length of the array) and we will check if the current number (number at indexi
) is greater than the next number ( number at indexi+1
). - If the current number (number at index
i
) is greater than the next number ( number at indexi+1
), that means the array is not sorted, and we will returnfalse
. - If the control came out of the for loop, that means the array is sorted and we will return
true
. - When we receive the
boolean
resultisSorted
we will print the output.
Conclusion
we can check if the primitive arrays are sorted or not by comparing the adjacent array elements.
The example Java programs used in the above article can be found at this GitHub repository, blogpost-coding-examples/java-programs/second-largest-element-in-an-array/.
Please write your thoughts in the comment section below.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!