I want to create a method that returns 0 if array not sorted returns 1 if it is sorted in ascending order and returns -1 if its sorted in descending order
This is what I have done so far:
public static int isSorted(int[] intArray) {
int end = intArray.length - 1;
int val = 0;
for (int i = 1; i < end; i++) {
if (intArray[0] < intArray[i]) {
val = 1;
}
else if (intArray[0] > intArray[i]) {
val = -1;
}
}
return v;
}
}
This returns 1 if its ascending and -1 if its descending. But if I create a random array it does not return 0. The question is how to check if both conditions fail, i.e., if its not sorted at all.
valvariable at every loop iterationi < endwithi <= endsince end is the length but already subtracted one1,2,3is ascending,1,1,2is non-descending).