I need to check if array was sorted strictly descendant. I wrote following code
public boolean isSortedDescendant(int [] array){
if ((array.length == 0) || (array.length == 1)) {
return true;
} else {
for(int i = 0; i < array.length - 1; i++){
if (array[i] > array[i + 1]) {
return true;
}
}
return false;
}
}
But it not working correctly. for
int[] array2 = {3, 2, 2};
at least. I spend a lot of time for different approaches, but without any luck.
You should only return true after checking all the pair of elements:
public boolean isSortedDescendant(int [] array){
if ((array.length == 0) || (array.length == 1)) {
return true;
} else {
for(int i = 0; i < array.length - 1; i++){
if (array[i] <= array[i + 1]) {
return false;
}
}
return true;
}
}
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