java - 覆盖比较器接口(interface)的比较方法时使用什么排序算法?

标签 java algorithm sorting collections

 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/

相关文章:

java - 服务运行时如何查看tomcat日志?

algorithm - 自选团队

java - 为什么 Java 的 String 中的 hashCode() 使用 31 作为乘数?

javascript - react 虚拟网格排序图标

Java 11 根据枚举值选择一种方法应用于流

Java快速查找点是否落在国家(多边形)内的方法

java - 构造函数 ClassRoll(String f) 赋值帮助,我需要确定为什么无法显示滚动

c++ - 面试算法查询

MYSQL 对组中的项目进行排序,然后对组进行排序

php - 如何查询数据库的中间值并按最接近的排序?