Java Array Program · A06
Check If an Array Is Sorted in Java
An array is sorted when every element is no greater than the next one. Compare neighbours, stop at the first pair out of order, and do not sort anything.
Problem Statement
Write a method that returns true if an int array is sorted in ascending order, otherwise false.
Equal neighbours are allowed, so [3, 3, 5] is sorted. Empty and single-element arrays count as sorted.
Input/Output Examples
[3, 3, 5, 9]OutputtrueExplanation: every element is less than or equal to the next.[1, 4, 2, 8]OutputfalseExplanation: 4 is greater than 2, the element after it.[]OutputtrueExplanation: there is no pair that can be out of order.Visual Explanation
[1, 4, 2, 8]: each tag compares an element with the one before it. The first broken pair (amber) ends the check.There are only n - 1 neighbouring pairs to check, and one broken pair is enough to answer false. That is why the loop starts at index 1 and compares arr[i - 1] with arr[i].
Approaches
Both approaches compare neighbouring elements and stop early. One uses a loop; the other uses allMatch() on a stream of indexes.
Approach 1: Loop Over Neighbours (Recommended)
Idea: for each index i from 1, check that arr[i - 1] <= arr[i].
- Start the loop at index 1.
- If
arr[i - 1] > arr[i], returnfalse. - If the loop finishes, return
true.
Time: O(n) · Space: O(1).
Approach 2: IntStream.allMatch()
Idea: stream the indexes 1 to n - 1 and require that every one satisfies the order condition.
- Create the indexes with
IntStream.range(1, arr.length). - Call
allMatch(i -> arr[i - 1] <= arr[i]). allMatch()returnstruefor an empty stream.
Time: O(n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Loop Over Neighbours (Recommended)
Starting at 1 keeps i - 1 inside the array, and the empty and single-element cases skip the loop entirely.
import java.util.Arrays;
public class SortedCheckLoop {
// Ascending means every element is <= the next one; equal neighbours are allowed
static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {
return false; // first pair out of order
}
}
return true;
}
public static void main(String[] args) {
int[][] inputs = {{3, 3, 5, 9}, {1, 4, 2, 8}, {}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + isSorted(arr));
}
}
}
[3, 3, 5, 9] -> true [1, 4, 2, 8] -> false [] -> true
Time complexity: O(n) · Space complexity: O(1).
Approach 2: IntStream.allMatch()
allMatch() short-circuits, so it stops at the first broken pair, just like the loop.
import java.util.Arrays;
import java.util.stream.IntStream;
public class SortedCheckStream {
// allMatch() stops at the first index that breaks the order
static boolean isSorted(int[] arr) {
return IntStream.range(1, arr.length)
.allMatch(i -> arr[i - 1] <= arr[i]);
}
public static void main(String[] args) {
int[][] inputs = {{3, 3, 5, 9}, {1, 4, 2, 8}, {}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + isSorted(arr));
}
}
}
[3, 3, 5, 9] -> true [1, 4, 2, 8] -> false [] -> true
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Loop Over Neighbours | O(n) | O(1) |
| IntStream.allMatch() | O(n) | O(1) |
Here n is the number of elements. In the worst case, when the array is sorted, all n - 1 pairs are compared; otherwise the check stops earlier.
Interview Tips and Recommended Approach
- Clarify strict or non-strict order. Use
<=when duplicates are allowed and<when the order must be strictly increasing. - Do not sort and compare. Copying, sorting and calling
Arrays.equals()works but costsO(n log n)time andO(n)memory. - Watch the index range. A loop from
0ton - 1that readsarr[i + 1]must stop one element earlier. - Know the descending variant: flip the comparison, or check both directions in one pass.
Recommended Approach: Loop Over Neighbours
Why: it answers in a single pass with constant memory and stops at the first problem. Its index handling is simple to explain and easy to adapt to strict or descending order.