Is Array Sorted?
easySave
Array
Given an array of numbers, determine if it is sorted in non-decreasing order. Return true if the array is sorted, otherwise return false.
Example 1
Input
[ 1, 2, 2, 3 ]
Output
true
Explanation. The numbers are in non-decreasing order, thus the output is true.
Example 2
Input
[ 7, 5, 3, 1 ]
Output
false
Explanation. The numbers are not sorted, as the sequence decreases.
Example 3
Input
[]
Output
true
Explanation. An empty array is considered sorted.
Example 4
Input
[ 15.6, 15.6, 16.1 ]
Output
true
Explanation. The sequence of floating-point numbers is non-decreasing.
Follow-up: How would you modify your solution if you needed to check for strictly increasing order?
Constraints:
The array may contain integers or floating-point numbers. It can also be empty, which should be considered as sorted.
- Views
- 3