What sorting algorithm used while overriding compare method of comparator interface?

隐身守侯 提交于 2019-12-19 11:42:05

问题


 Collections.sort(ar, new Comparator<Intervals>() {
                      @Override
                        public int compare(Intervals o1, Intervals o2) {                
                                return (Integer.valueOf(o1.getEnd()))
                                        .compareTo(Integer.valueOf(o2.getEnd()));
                        }
                    });

Hi all, I have the above code in java. Here, ar is a list and Intervals is a class with 2 integer variables : Start and End. I wanted to know what sorting algorithm is followed when we override the compare method of Comparator interface as above. I know, by default Collections.sort() and Arrays.sort() use Timsort algorithm. Any help will be appreciated. Thanks a lot in advance.


回答1:


Collections.sort() uses a variation of Timsort.

From the 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.

Note that the Collections.sort() algorithms gets a "black box" comparator, and uses the value it yields for each compare - without caring what is going on behind the scenes of the comparator.




回答2:


The Comparator and Comparable interface don't do any sorting, so there is no sorting algorithm there. They just compare two Objects, something you need if you want to sort a list of those objects.



来源:https://stackoverflow.com/questions/30638617/what-sorting-algorithm-used-while-overriding-compare-method-of-comparator-interf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!