覆盖比较器接口的比较方法时使用什么排序算法?
What sorting algorithm used while overriding compare method of comparator interface?
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 是一个 class,有 2 个整数变量:Start 和 End。我想知道当我们像上面那样重写Comparator接口的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()
算法得到一个 "black box" 比较器,并使用它为每次比较产生的值 - 不关心比较器幕后发生的事情。
Comparator 和 Comparable 接口不进行任何排序,因此那里没有排序算法。他们只是比较两个对象,如果你想对这些对象的列表进行排序,你需要一些东西。
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 是一个 class,有 2 个整数变量:Start 和 End。我想知道当我们像上面那样重写Comparator接口的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()
算法得到一个 "black box" 比较器,并使用它为每次比较产生的值 - 不关心比较器幕后发生的事情。
Comparator 和 Comparable 接口不进行任何排序,因此那里没有排序算法。他们只是比较两个对象,如果你想对这些对象的列表进行排序,你需要一些东西。