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.
答案 0 :(得分:4)
Collections.sort()使用Timsort的变体。
来自javadocs:
该实现改编自Tim Peters的Python列表 (TimSort)。它使用了Peter McIlroy的“乐观排序”中的技术 和“信息理论复杂性”,“第四次会议论文集” 年度ACM-SIAM离散算法研讨会,第467-474页,1月 1993。
请注意,Collections.sort()
算法得到一个“黑盒子”比较器,并使用它为每次比较产生的值 - 而不关心比较器幕后的情况。
答案 1 :(得分:1)
Comparator和Comparable接口不进行任何排序,因此没有排序算法。他们只是比较两个对象,如果你想对这些对象的列表进行排序,你需要它。