簡體   English   中英

已排序? 檢查數組是否在 Java 中按升序或降序排序

[英]isSorted? Check array if is sorted in ascending or descending order in Java

有人可以幫我檢查我的代碼是否正確或幫助我知道是否有其他方法可以解決這個問題我試圖檢查一個數組是按升序還是降序排列,如果不是,則返回 1,然后返回 0; 起初,我創建了一些以遞增順序和順序對數組進行排序的方法以及另一種用於遞減的方法,然后我使用這些方法與原始數組進行比較,如果它已排序。 我使用了下面的代碼:

public class IsSorted {

public static void main(String[] args){
    int[] list ={4,3,2,1};
    System.out.println(isSorted(list));

}

public static int isSorted(int[] a){

    if(a.length==0){
        return 1;
    }
    if(a.length==1){
        return 1;
    }

    int[] holdingArray=new int[a.length];

    for (int i =0; i<a.length; i++){
        holdingArray[i]=a[i];
    }


    int[] virtualIncreasedArray= new int[holdingArray.length];
    int[] virtualDecreasedArray= new int[holdingArray.length];
    sortIncrease(holdingArray);

    for(int i=0; i<holdingArray.length;i++){
        virtualIncreasedArray[i]=holdingArray[i];
    }
    sortDecrease(holdingArray);

    for(int i=0; i<holdingArray.length;i++){
        virtualDecreasedArray[i]=holdingArray[i];
    }

    //check if array is decreasing
    for(int i=0; i<virtualDecreasedArray.length;i++){
        if(virtualDecreasedArray[i]!=a[i]&&virtualIncreasedArray[i]!=a[i]){
            return 0;
        }

    }
    //check if array is increasing


    return 1;
}


static void sortIncrease(int[] a){
    for(int unsorted=a.length-1; unsorted>0; unsorted--){
        for(int i=0; i<unsorted;i++){
            if(a[i]>a[i+1]){
                swap(a,i,i+1);
            }
        }
    }
}


static void sortDecrease(int[] a){
    for(int unsorted=a.length-1; unsorted>0; unsorted--){
        for(int i=0; i<unsorted; i++){
            if(a[i]<a[i+1]){
                swap(a,i,i+1);
            }
        }
    }
}

static void swap(int[] a, int i, int j){
    if(i==j){
        return;
    }
    int temp = a[i];
    a[i]=a[j];
    a[j]=temp;
}

}

為了進行准確的驗證,應執行以下操作,因為需要考慮重要的附帶情況。

  • 檢查是否有任何列表以一些相等的值開始。
  • 確定值首先不同的起始索引。
  • 如果所有值都相等,則立即返回true
  • 請注意,在所有值都相等的最壞情況下,需要檢查整個數組(沒有最后一個值,此后它可能是升序或降序)。
int[] sortedAscending = { 1, 1, 3, 4, 7, 10, 11, 15, 15 };
int[] sortedDescending = { 22, 22, 12, 8, 8, 8, 5, 2, 1 };
int[] sortedBoth = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int[] unsorted = { 2, 1, 2, 19, 19, 2, 4 };

System.out.println(isSorted(sortedAscending));
System.out.println(isSorted(sortedDescending));
System.out.println(isSorted(sortedBoth));
System.out.println(isSorted(unsorted));

印刷

true
true
true
false

檢查方法。

    
public static boolean isSorted(int[] arr) {
    
    int start = 0;
    // if the lists start with equal values, need to 
    // determine a starting point.
    while (arr[start] == arr[start+1]
            && start++ < arr.length - 2);
    if (start >= arr.length - 2) {
        // all but the last the same value, its sorted 
        return true;
    }
    boolean asc = arr[start] < arr[start + 1];
    for (int i = start; i < arr.length - 1; i++) {
        if (asc) {
            //check ascending
            if (arr[i] > arr[i + 1]) {
                return false;
            }
            // check descending
        } else if (arr[i] < arr[i + 1]) {
            return false;
        }
    }
    return true;
}

由於您要求另一種方法來做到這一點,這里有一種不同的方法。

你可以做的是:

  • 根據前 2 個元素(如果存在)確定數組是(假定)按升序還是降序排序
  • 在確定假定的排序時考慮相等的值(感謝您指出@WJS)
  • 然后,根據確定的內容,檢查陣列的 rest 的順序是否正確

更新示例:

public static void main(String[] args) {
    int[] sortedAsc = { 1, 2, 3, 4, 5 };
    int[] sortedDesc = { 5, 4, 2, 1 };
    int[] unsortedArray = { 1, 8, 2, 4 };
    int[] allEqual = { 3, 3, 3, 3, 3 };
    int[] firstEqual = { 2, 2, 3, 2 };

    System.out.println(isSorted(sortedAsc));
    System.out.println(isSorted(sortedDesc));
    System.out.println(isSorted(unsortedArray));
    System.out.println(isSorted(allEqual));
    System.out.println(isSorted(firstEqual));
}

public static boolean isSorted(int[] arr) {
    boolean isAscending = false;

    if (arr.length < 2) { // if the array has less than 2 elements, must be sorted
        return true;
    }

    if (arr[0] < arr[1]) { // do we think this array is sorted ascending?
        isAscending = true;
    } else {
        int index = 0;
        while (arr[index] == arr[index + 1] && index++ < arr.length - 2) {
            // keep checking for sorting if array consists of equal values
            if (index >= arr.length - 2) {
                return true; // whole array consists of equal values
            }
        }
        // now that equal values were skipped, check for sorting again
        isAscending = arr[index] < arr[index + 1]; 
    }

    // check all elements of the array
    for (int i = 0; i < arr.length - 1; i++) {
        if (isAscending) {
            if (arr[i] > arr[i + 1]) {
                return false;
            }
        } else {
            if (arr[i] < arr[i + 1]) {
                return false;
            }
        }
    }
    return true;
}

Output:

true
true
false
true
false

您的代碼的旁注:

  • 而不是返回 int (0, 1),您的isSorted()方法絕對應該返回boolean
  • holdingArray沒有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM