- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个比较器方法有什么问题?
我已阅读: Java error: Comparison method violates its general contract
并理解,如果 c1 > c2,且 c2 > c3,则 c1 > c3。我相信上面的内容应该是正确的。
getMaxCosine() 返回 0..1 之间的值,第二次排序是按照卡片中文本的长度排序,越长排名越高。
public int compare(Card c1, Card c2) {
if (getMaxCosine(c1) > getMaxCosine(c2)) {
return -1;
} else if (getMaxCosine(c1) == getMaxCosine(c2)) {
return getMatchingText(c1).length() >= getMatchingText(c2).length() ? -1 : 1;
} else {
return 1;
}
}
最佳答案
我认为您的问题出在您的 if
-else
block 中:
else if (getMaxCosine(c1) == getMaxCosine(c2)) {
return getMatchingText(c1).length() >= getMatchingText(c2).length() ? -1 : 1;
}
如果 getMatchingText(c1).length()
等于 getMatchingText(c2).length()
则返回 -1
。这会产生“不稳定”的排序:换句话说,两个具有相同值的对象在排序后顺序将颠倒。此外,对于在此比较器下相等的 Card
,您应该返回 0。我建议将 >=
比较更改为 if
-else
block 中的 >
:
else if (getMaxCosine(c1) == getMaxCosine(c2)) {
if (getMatchingText(c1).length() == getMatchingText(c2).length()) return 0;
return getMatchingText(c1).length() > getMatchingText(c2).length() ? -1 : 1;
}
关于java - TimSort 违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55938101/
我想了解Timsort。包括所有细节,所以像“只需在 Java 或 Python 中调用 .sort()”这样的答案不是我要找的。 我已经阅读了 Wikipedia article , Java im
来自: http://svn.python.org/projects/python/trunk/Objects/listsort.txt 和: http://en.wikipedia.org/wiki
这个比较器方法有什么问题? 我已阅读: Java error: Comparison method violates its general contract 并理解,如果 c1 > c2,且 c2
Java 7 changed the sorting algorithm这样它就会抛出一个 java.lang.IllegalArgumentException: "Comparison method
我想对列表进行排序并观察/可视化 Python 的排序算法 Timsort正在移动元素。 第一次尝试 : list 的子类每次更改后都会打印出来: class List(list): def
这是我得到的堆栈跟踪 Caused by: java.lang.IllegalArgumentException: Comparison method violates its general con
TimSort 是一种将在 Java 7 中默认用于排序的算法。 我找到了这个来源,但我不明白应该调用哪个方法,因为它们都是私有(private)的。有人能看懂吗?谢谢。 http://cr.open
我正在尝试在 Swift 中实现 TimSort。我已经提到了这两个链接:This和 this我转换为 Swift 的代码是: import UIKit class ViewController: U
我已经实现了 TimSort,但我确实需要能够按不同的字段进行排序。例如。按字段 2,然后 1,然后 3 排序。我一般知道如何执行此操作,如果先前给定的排序字段相等,则按下一个字段排序,但我正在寻找具
我正在开发一个启发式例程,需要维护一个排序的序列/数组/列表。我听说 timsort 可以快速维护具有已排序子序列的序列的顺序。 另一方面,我需要的很简单。在对序列进行初步排序后,我需要在每个步骤中重
我试图理解 Timsort 算法,但我无法理解实现堆栈不变性的原因: A > B+C B > C 根据 this文档, We would like to delay merging as long a
使用 Java 8 u151 我们正在进行以下排序: Collections.sort(srs, (sr1, sr2) -> sr1.getValidStartTime().compareTo(sr2
在 TimRun document 的“计算最小运行”部分,它给出了为 N=2112 数组选择 minrun 的好坏示例。它指出使用 minrun = 32 是低效的,因为 runs of lengt
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Timsort is an adaptive, stable, natural mergesort. It has supernatural performance on many kinds of
我找到了 Timsort 的 Swenson C 实现: https://github.com/swenson/sort在一个较旧的 SO 问题中提到过。 我遇到两个问题: 1)要使用它,我需要定义适
我正在研究“timsort”算法,以便对我相当大的数据集进行排序: http://timsort4net.codeplex.com/ 通常我使用 Array.Sort(Keys, Items),其中
我在使用我自己的比较器实现通过 Collections.sort() 对我的集合进行排序时遇到问题。抛出的异常是-->“IllegalArgumentException:比较方法违反了其一般契约!在我
// // main.cpp // timsort // // Created by Atharva Koli on 2019/1/27. // Copyright © 2019 Athar
我一直在寻找一种为 C++ 实现 Timsort 的方法 (Implementation found on Github)使用多线程,我尝试在此过程中使用。我确信我使用的是正确的编译器标志,但每当我尝
我是一名优秀的程序员,十分优秀!