Check if an Array is Sorted

We will write a program to check if an array is sorted

Check if an Array is Sorted

Given an array of size n, the task is to find if this array is sorted or not (non-decreasing order)

We start traversing from the second element, and for every iteration we verify if the current element is less than previous element, if so return false.

boolean isSorted(int[] arr) {
	for (int i = 1; i < arr.length; i++) {
		if (arr[i] < arr[i - 1]) {
			return false;
		}
	}
	return true;
}

Time Complexity is O(n), Space Complexity: O(1)