Why does Collections.sort call Comparator twice with the same arguments?

后端 未结 3 1274
感情败类
感情败类 2021-01-02 20:53

I\'m running an example to understand the behavior of Comparator in Java.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparat         


        
3条回答
  •  有刺的猬
    2021-01-02 21:24

    It depends on sorting algorithm, on how many times it calls compare method. Once we call Collections.sort() method, it goes to the implementation of sorting used in Collections.sort().

    Collections.sort() implementation uses merge sort. According to the Javadoc, only primitive arrays are sorted using Quicksort. Object arrays are sorted with a Mergesort as well.

提交回复
热议问题