Check Whether an Array Is Sorted
Determine whether an array is sorted in non-decreasing order in a single pass.
Walk the array once and compare each element with the one before it. If any element is smaller than its predecessor, the array is not sorted in ascending order, so return false immediately. If you reach the end without finding such a dip, return true.
Problem Statement
Given an array of integers, decide whether it is sorted in non-decreasing (ascending) order — every element is greater than or equal to the one before it.
Return true if the array is sorted and false otherwise. You only need one pass;
a single out-of-order adjacent pair is enough to answer false.
Input: An array arr of length n.
Output: A boolean: true if sorted ascending, false otherwise.
Examples
Input: [1, 2, 3, 4, 5]
Output: trueEvery element is greater than or equal to the previous one.
Input: [1, 3, 2, 4]
Output: falseThe pair 3 then 2 goes down, so the array is not sorted ascending.
Constraints
0 <= n <= 10^6Equal adjacent values are allowed (non-decreasing order)
Think Before You Code
Reveal the questions to ask yourself first
- Which pairs of elements do you actually need to compare?
- Can you stop early as soon as you find a violation?
- Should equal adjacent values count as sorted or not?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Scan once, checking each adjacent pair.
- Loop
ifrom 1 ton - 1. - If
arr[i] < arr[i - 1], the order is broken — returnfalse. - If the loop completes with no violation, return
true.
Using < (not <=) means equal neighbours are treated as still sorted, which is the
usual non-decreasing definition. Arrays of length 0 or 1 are trivially sorted.
Dry Run
Walk through the example step by step
Checking [1, 3, 2, 4]:
i | arr[i-1] | arr[i] | arr[i] < arr[i-1]? | action
--+----------+--------+--------------------+--------------
1 | 1 | 3 | no | continue
2 | 3 | 2 | yes | return false
---------------------------------------------- result false
Solution
Reveal the full Java solution
public class IsSorted {
public static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isSorted(new int[]{1, 2, 3, 4, 5})); // true
System.out.println(isSorted(new int[]{1, 3, 2, 4})); // false
}
}
Sortedness is a purely local property: an array is ascending exactly when every adjacent pair is in order. So checking neighbours is sufficient, and returning early on the first dip makes the scan stop as soon as the answer is known — O(n) worst case and often faster.
O(n)Space: O(1)Common Mistakes
- Comparing with `<=` and rejecting arrays that contain equal adjacent values.
- Starting the loop at index 0 and reading `arr[-1]`, causing an out-of-bounds error.
- Continuing to scan after a violation instead of returning false immediately.
Edge Cases to Test
- Empty array [] and single-element array [7] are both sorted (true).
- Arrays with equal adjacent values like [2, 2, 3], which are still sorted.
- A strictly descending array like [5, 4, 3], which returns false at the first pair.
Interview Follow-Ups
- How would you also detect descending order in the same pass?
- How would you check whether the array is sorted and has no duplicates?
- How would you return the first index where the order breaks?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →