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.
Approach used below is as follows to solve the problem − Take an array arr[] as an input and initialize n with the size of an array. Check if we reached the starting of an array, return true. Check if the previous element of an array is not smaller than the next element, return false. Decrement n and goto step 2.
sort() in Java with examples. Array class is a class containing static methods that are used with arrays in order to search, sort, compare, insert elements, or return a string representation of an array.
php $sort = array( 0 => 50, 1 => 100, 2 => 150 ); $default = $sort; sort($sort); $flag = true; foreach($sort as $key=>$value) if($value!= $default[$key]) $flag = false; if($flag) echo "Already sorted"; else echo "Not Already sorted"; ?>
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;
}
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.
Array.prototype.reduce()
You can use .every
let isSorted = array.every((v, i) => (i === 0 || v <= array[i - 1]))
|| array.every((v, i) => (i === 0 || v >= array[i - 1]))
function isArraySorted(arr) {
for (let i=0;i<arr.length;i++) {
if (arr[i+1] && (arr[i+1] > arr[i])) {
continue;
} else if(arr[i+1] && (arr[i+1] < arr[i])) {
return false;
}
}
return true;
}
This worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With