[英]Find out if the numbers in an array are in increasing order (or) decreasing order (or) no order?
Question : Find out if the numbers in an array are in increasing order
(或)遞減順序(或)遞增和遞減(或)遞減和遞增(或)無序? 測試用例 1:[1,2,3] ->“增加”測試用例 2:[3,2,1] ->“減少”測試用例 3:[1,2,3,2,1] ->“增減” 測試用例 4 : [3,2,1,2,3] -> “增減” 測試用例 5 : [1,2,3,2,1,2] (0r) [1,1 ,1,1,1] (or) [1] (or) [] ->“無序”
我創建了一個方法來完成你想要完成的事情。
如果數組正在增加,下面的方法將返回1
,如果數組為空,則返回0
; 無序; or 包含單個元素,如果數組減少,則為-1
。
public static int isArrayIncreasingOrDecreasing(int[] array) {
boolean increasing = true;
boolean decreasing = true;
if (array == null || array.length <= 1) {
return 0;
}
for (int i = 1; i < array.length; i++) {
if (increasing) {
if (array[i] < array[i - 1]) {
increasing = false;
}
}
if (decreasing) {
if (array[i] > array[i - 1]) {
decreasing = false;
}
}
}
if (increasing) {
return 1;
} else if (decreasing) {
return -1;
}
return 0;
}
要在您的主要方法中實現此方法,您將執行以下操作。
public static void main(String[] args) {
int result = isArrayIncreasingOrDecreasing(new int[]{1, 2, 3, 4, 5});
if (result == -1) {
System.out.println("The array is decreasing in order.");
} else if (result == 0) {
System.out.println("The array is in no order.");
} else {
System.out.println("The array is increasing in order.");
}
}
當數組中的所有元素都相等或數組中的元素數小於2
時,就會出現邊緣情況。 在這種情況下,我們不能稱這個數組是無序的,它肯定是有序的(因為不需要排序——它已經按順序排列了),但無法確定順序是增加還是減少。
將這種情況描述為“增加和減少”有點違反直覺。
為了解決這個問題,我們可以維護兩個boolean
變量:一個表示數組是否有序,另一個表示順序的類型(升序或降序)。 在遍歷給定數組時,我們需要檢查下一對元素( previous 和 current )是否匹配順序,如果不是,我們將isOrdered
設置為false
並中斷循環。
這就是它可能實現的方式。
public static void determineOrder(int[] arr) {
boolean isAsc = arr[0] < arr[arr.length - 1]; // if array is sorted in ascending order the first element has to be less than the last element
boolean isOrdered = true;
for (int i = 1; i < arr.length; i++) { // iteration starts from 1 because at each iteration step we need to compare `current` and `previous` elements
int previous = arr[i - 1];
int current = arr[i];
if (previous > current && isAsc || previous < current && !isAsc) { // the order doesn't match
isOrdered = false;
break;
}
}
// printing the result
if (!isOrdered) {
System.out.println("Unordered");
} else if (arr[0] == arr[arr.length - 1]) {
System.out.println("Impossible to determine the order");
} else if (isAsc) {
System.out.println("Array is sorted in Ascending order");
} else {
System.out.println("Array is sorted in Descending order");
}
}
main()
public static void main(String[] args) {
determineOrder(new int[]{1, 2, 3, 3, 3}); // ASC
determineOrder(new int[]{7, 5, 3, 3, 5}); // unordered
determineOrder(new int[]{7, 8, 9, 8, 8}); // unordered
determineOrder(new int[]{7, 5, 3, 3, 1}); // DSC
determineOrder(new int[]{9, 9, 9, 9, 9}); // impossible to determine the order
}
輸出:
Array is sorted in Ascending order
Unordered
Unordered
Array is sorted in Descending order
Impossible to determine the order
測試數組是否按升序排列的一種方法是按升序排序,如果保持不變,則按升序排列。 下降也是如此。
現在,如果一個數組在按升序和降序排序時保持不變,那么它在這兩個順序中。 當所有數組元素都相等且數組為空時,就會發生這種情況。
最后,如果一個數組在按升序和降序排序時發生了變化,那么它就不是這些順序。 它缺乏秩序。
有了這個關於這個問題的工具性觀點,人們甚至不必知道數組的內容,只需知道它是否發生了變化。 並且沒有需要考慮的極端情況。
我建議的測試在概念上可以支持對問題的思考。 它也可以使用標准排序在任何編程語言中實現,因為對於什么構成升序/降序存在壓倒性的共識。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.