【问题标题】:isSorted? Check array if is sorted in ascending or descending order in Java已排序?检查数组是否在Java中按升序或降序排序
【发布时间】:2021-03-27 04:56:04
【问题描述】:

有人可以帮我检查我的代码是否正确或帮助我知道是否有其他方法可以解决这个问题;起初,我创建了一些按递增顺序和顺序对数组进行排序的方法以及另一种用于递减的方法,然后我使用这些方法与原始数组进行比较,如果它已排序。我使用了下面的代码:

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

}

【问题讨论】:

  • 也许你应该试着想想当你进行排序时会发生什么。也许打印排序前后的值?
  • 你为什么不返回一个布尔值(真/假)而不是一个整数?
  • 是的,我已经改变了,谢谢

标签: java arrays sorting arraylist


【解决方案1】:

由于您要求另一种方法来做到这一点,这里是一种不同的方法。

你可以做的是:

  • 根据前 2 个元素(如果存在)确定数组是(假定)按升序还是降序排序
  • 在确定假定的排序时考虑相等的值(感谢 @WJS 指出这一点)
  • 然后,根据确定的内容检查数组的其余部分是否正确

更新示例:

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

输出:

true
true
false
true
false

代码旁注:

  • 您的 isSorted() 方法应该绝对返回 boolean,而不是返回 int (0, 1)。
  • holdingArray 没有任何意义。

【讨论】:

  • 很遗憾,您的方法有问题。如果数组以相等的值开始或具有所有相同的值怎么办?此外,如果数组的元素少于 3 个,则还必须对其进行排序(一种或另一种)。
  • @WJS 感谢您指出这一点。为您的回答 +1。
【解决方案2】:

为了进行准确的验证,应执行以下操作,因为有重要的附带情况需要考虑。

  • 检查是否有任何列表以一些相等的值开头。
  • 确定值首先不同的起始索引。
  • 如果所有值都相等,则立即返回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;
}

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 2018-03-18
    相关资源
    最近更新 更多