Collections.sort(ar, new Comparator<Intervals>() {
@Override
public int compare(Intervals o1, Intervals o2) {
return (Integer.valueOf(o1.getEnd()))
.compareTo(Integer.valueOf(o2.getEnd()));
}
});
大家好,我在java中有上面的代码。在这里,ar 是一个列表,Intervals 是一个具有 2 个整数变量的类:Start 和 End。我想知道当我们像上面那样重写Comparator接口(interface)的compare方法时,遵循的是什么排序算法。我知道,默认情况下 Collections.sort() 和 Arrays.sort() 使用 Timsort 算法。任何帮助将不胜感激。非常感谢。
最佳答案
Collections.sort() 使用 Timsort 的变体.
来自javadocs :
The implementation was adapted from Tim Peters's list sort for Python ( TimSort). It uses techiques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.
请注意,Collections.sort()
算法有一个“黑匣子”比较器,并使用它为每次比较产生的值——而不关心比较器幕后发生的事情。
关于java - 覆盖比较器接口(interface)的比较方法时使用什么排序算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30638617/