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

前端 未结 2 1544
粉色の甜心
粉色の甜心 2021-01-15 20:55
 Collections.sort(ar, new Comparator() {
                      @Override
                        public int compare(Intervals o1, Intervals o2) {            


        
相关标签:
2条回答
  • 2021-01-15 21:15

    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.

    0 讨论(0)
  • 2021-01-15 21:17

    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.

    0 讨论(0)
提交回复
热议问题