简体   繁体   English

已排序? 检查数组是否在 Java 中按升序或降序排序

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

can someone help me check my codes if are right or help me know if there is any other is way to solve this question I was trying to check if an array is in ascending or descending order then return 1 if not then return 0;有人可以帮我检查我的代码是否正确或帮助我知道是否有其他方法可以解决这个问题我试图检查一个数组是按升序还是降序排列,如果不是,则返回 1,然后返回 0; at first I created some method for sorting array in increasing order and order and another method for decreasing and then I used those method to compare with the original array if it is sorted.起初,我创建了一些以递增顺序和顺序对数组进行排序的方法以及另一种用于递减的方法,然后我使用这些方法与原始数组进行比较,如果它已排序。 I used the code below:我使用了下面的代码:

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;
}

} }

For an accurate verification, the following should be done as there are important side cases to consider.为了进行准确的验证,应执行以下操作,因为需要考虑重要的附带情况。

  • Checking if any list starts out with some number of equal values.检查是否有任何列表以一些相等的值开始。
  • Determine the starting index where the values first differ.确定值首先不同的起始索引。
  • If all the values are equal, return true immediately.如果所有值都相等,则立即返回true
  • Note that in the worst case when all the values are equal, the entire array needs to be checked (sans the last value since then it could be either ascending or descending).请注意,在所有值都相等的最坏情况下,需要检查整个数组(没有最后一个值,此后它可能是升序或降序)。
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));

Prints印刷

true
true
true
false

The check method.检查方法。

    
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;
}

Since you asked for another way to do this, here is a different approach.由于您要求另一种方法来做到这一点,这里有一种不同的方法。

What you could do is:你可以做的是:

  • Determine whether the array is (supposedly) sorted in ascending or descending order based on the first 2 elements (if these exist)根据前 2 个元素(如果存在)确定数组是(假定)按升序还是降序排序
  • Consider equal values when determining the supposed sorting (thanks for pointing that out @WJS)在确定假定的排序时考虑相等的值(感谢您指出@WJS)
  • Then, check the rest of the array for the correct order, based on what was determined然后,根据确定的内容,检查阵列的 rest 的顺序是否正确

Updated Example:更新示例:

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: Output:

true
true
false
true
false

Sidenotes for your code:您的代码的旁注:

  • Instead of returning an int (0, 1), your isSorted() method should definitely return boolean .而不是返回 int (0, 1),您的isSorted()方法绝对应该返回boolean
  • There is no point in the holdingArray . holdingArray没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM