patternjavaMinor
Checking if numbers in array are sorted or reverse sorted
Viewed 0 times
reversearrayarenumberscheckingsorted
Problem
I would like to determine is an array of numbers, is always increasing (for all x, x+1 >= x) or always decreasing (for all x, x+1 =< x). If so return true. Another way to state the problem is, return true if the array is sorted or reverse sorted. Here is the code:
Any tips? Obviously duplicate values are allowed, so inputs such as 1,2,3,3,3,3....3,4,2,5,6 would be tricky.
I ask mainly out of curiosity, as the above solution would take at most 2 iterations through the array and I don't think it is possible to take less than 1, so it's not too bad :)
public static boolean isAlwaysIncreasingOrDecreasing(int[] arr) {
//check if decreasing
int previous = arr[0];
boolean decreasing = true;
for(int i = 1; i arr[i]) {
increasing = false;
break;
}
previous = arr[i];
}
//return true if always increasing or decreasing
if(decreasing || increasing) {
return false;
}
return true;
}Any tips? Obviously duplicate values are allowed, so inputs such as 1,2,3,3,3,3....3,4,2,5,6 would be tricky.
I ask mainly out of curiosity, as the above solution would take at most 2 iterations through the array and I don't think it is possible to take less than 1, so it's not too bad :)
Solution
Your version is slightly misnamed. It's actually
If you don't want to duplicate the checks, you can add a bit more logic.
This version only loops once, although it has three loops coded. It only runs one loop over any section of the array.
It also gets rid of the Boolean variables. It can return immediately since it only gets to the loops once it already knows which one to test.
isNonincreasingOrNondecreasing. It returns true for arrays where all the entries are the same value, which is neither increasing nor decreasing. If you don't want to duplicate the checks, you can add a bit more logic.
public static boolean isNonincreasingOrNondecreasing(int[] sequence) {
// clear out equal entries from the beginning
int i = 1;
while (i = sequence.length) {
return true;
}
int previous = sequence[i - 1];
if (i sequence[i]) {
for (; i sequence[i]) {
return false;
}
previous = sequence[i];
}
return true;
}This version only loops once, although it has three loops coded. It only runs one loop over any section of the array.
It also gets rid of the Boolean variables. It can return immediately since it only gets to the loops once it already knows which one to test.
Code Snippets
public static boolean isNonincreasingOrNondecreasing(int[] sequence) {
// clear out equal entries from the beginning
int i = 1;
while (i < sequence.length && sequence[i - 1] == sequence[i]) {
i++;
}
// if there are no entries left, then it is nonincreasing and nondecreasing
if (i >= sequence.length) {
return true;
}
int previous = sequence[i - 1];
if (i < sequence.length && previous > sequence[i]) {
for (; i < sequence.length; i++) {
if (previous < sequence[i]) {
return false;
}
previous = sequence[i];
}
return true;
}
for (; i < sequence.length; i++) {
if (previous > sequence[i]) {
return false;
}
previous = sequence[i];
}
return true;
}Context
StackExchange Code Review Q#133025, answer score: 3
Revisions (0)
No revisions yet.