Java方法确认排序是否有效。

huangapple go评论92阅读模式
英文:

Java method to confirm the sorting works

问题

我有一个包含100个整数的数组(从0到99),并且我实现了一个Comparable接口。我现在的任务是确认排序是否有效(尽管我知道它有效)。我的代码如下:

public static boolean confirmSorting(Guppy[] sortedArrayOfGuppies) {
    boolean confirmed = false;
    int maximumID = Integer.MAX_VALUE;
    for (Guppy guppy : sortedArrayOfGuppies) {
        if (guppy.getIdentificationNumber() < maximumID) {
            maximumID = guppy.getIdentificationNumber();
            confirmed = true;
        }
    }
    return confirmed;
}

但是即使对于未排序的数组,它也会返回true。我如何确保遍历数组中的每个对象?

不幸的是,https://stackoverflow.com/questions/18111231/how-to-check-if-array-is-already-sorted 提出的解决方案不起作用,因为并非所有整数都是唯一的(有些是相同的)。

英文:

I have an array of 100 ints (from 0 to 99) and I implement a Comparable interface. My task now is to confirm that sorting works (even though I know it does). My code is:

public static boolean confirmSorting(Guppy[] sortedArrayOfGuppies) {
    boolean confirmed = false;
    int maximumID = Integer.MAX_VALUE;
    for (Guppy guppy : sortedArrayOfGuppies) {
        if (guppy.getIdentificationNumber() &lt; maximumID) {
            maximumID = guppy.getIdentificationNumber();
            confirmed = true;
        }
    }
    return confirmed;
}

But it returns true even for unsorted arrays. How do I make sure that iterate through every single object in my array?

Unfortunately, the solution proposed in https://stackoverflow.com/questions/18111231/how-to-check-if-array-is-already-sorted doesn't work, not all ints are unique (some are the same)

答案1

得分: 1

假设它们首先是按顺序排列的。一旦发现不按顺序排列的元素,立即返回。在遇到第一个错误后无需继续检查。这只做了简单的验证,不会打印任何信息。如果按升序排列则返回 true,否则返回 false。它还假定 ID 编号是一个 int

public static boolean confirmSorting(Guppy[] sortedArrayOfGuppies) {
    for (int i = 0; i < sortedArrayOfGuppies.length-1; i++) {
        int id1 = sortedArrayOfGuppies[i].getIdentificationNumber();
        int id2 = sortedArrayOfGuppies[i+1].getIdentificationNumber();
        if (id1 > id2) {
            return false;
        }
    }
    return true;
}

注意,你可能希望传入一个 comparatorflag,以便可以验证升序和降序排序。

英文:

Assume they are in sorted order first. Then return as soon as you find ones that aren't. No need to keep checking upon encountering the first failure. This does a simple verification and does not print any information. It returns true if sorted in ascending order and false otherwise. It also presumes the id number is an int.

public static boolean confirmSorting(Guppy[] sortedArrayOfGuppies) {
    for (int i = 0; i &lt; sortedArrayOfGuppies.length-1; i++) {
        int id1 = sortedArrayOfGuppies[i].getIdentificationNumber();
        int id2 = sortedArrayOfGuppies[i+1].getIdentificationNumber();
        if (id1 &gt; id2) {
            return false;
        }
    }
    return true;
}

Note that you may want to pass in a comparator or flag so you can verify both ascending and descending sorts.

huangapple
  • 本文由 发表于 2020年10月12日 06:46:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64309729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定