so how to make such logic
int[] arr = {2, 5, 3};
if (/* arr is sorted */)
....
else
...
Its bad that method Array.sort is void
so how to make such logic
int[] arr = {2, 5, 3};
if (/* arr is sorted */)
....
else
...
Its bad that method Array.sort is void
You don't need to sort your array to check if it's sorted. Loop over each consecutive pair of elements and check if the first is less than the second; if you find a pair for which this isn't true, the array is not sorted.
boolean sorted = true;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i+1]) {
sorted = false;
break;
}
}
public static <T>
boolean isArraySorted(T[] elements, Comparator<? super T> cmp) {
int n = elements.length;
for (int i = 1; i < n; ++i) {
if (cmp.compare(elements[i-1], elements[i]) > 0) { return false; }
}
return true;
}
java.lang.reflect.Array along with @SuppressWarnings("unchecked") which would defeat all the generic safety checks you get from the compiler. I think overloading for primitive types is a good idea.Well you can check it in O(n) worst case linear time. A non-sorted array (assuming you mean sorting in ascending order) will have a trip point. That is at some point arr[i] > arr[i+1]
All you need to do is
boolean is_array_sorted(int arr[]) {
for(int i=0; i < arr.len-1; i++) {
if(arr[i] > arr[i+1]) {
return false;
}
}
return true;
}
Just change the > to < if your array sort is supposed to be descending order
public static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (a[i + 1] < a[i]) {
return false;
};
}
return true;
}
A shorter version:
[0,1,2,3,4].reduce((a,v) => (a!==false) && (a <= v) ? v : false, -Infinity)
[4,3,1,2,0].reduce((a,v) => (a!==false) && (a >= v) ? v : false, +Infinity)
Be careful, as in some cases it isn't effective because it will loop through entire array without breaking prematurely.
A slight variation of the generics version by Mike Samuel but using Comparable<T> instead of Comparator<T>:
public static <T extends Comparable<? super T>> boolean isSorted(T[] elements) {
int n = elements.length;
for (int i = 1; i < n; ++i) {
if (elements[i - 1].compareTo(elements[i]) > 0) {
return false;
}
}
return true;
}
Use .some() (or .every()) to return the result as soon as possible:
const isArraySortedAscending = (array) => array.length < 2 || !array.slice(1).some((v, i) => (v < array[i]))
const isArraySortedDescending = (array) => array.length < 2 || !array.slice(1).some((v, i) => (v > array[i]))
Test:
const isArraySortedAscending = (array) => array.length < 2 || !array.slice(1).some((v, i) => (v < array[i]))
const isArraySortedDescending = (array) => array.length < 2 || !array.slice(1).some((v, i) => (v > array[i]))
console.log(isArraySortedAscending([1, 2, 3])) // true
console.log(isArraySortedDescending([1, 2, 3])) // false
console.log(isArraySortedAscending([3, 2, 1])) // false
console.log(isArraySortedDescending([3, 2, 1])) // true
console.log(isArraySortedAscending([1, 3, 2])) // false
console.log(isArraySortedDescending([1, 3, 2])) // false
console.log(isArraySortedAscending([1])) // true
console.log(isArraySortedDescending([1])) // true