In Java Arrays.sort() for primitive type uses quick sort. On the other hand Arrays.sort() for objects uses Merge sort. And, same goes for Collection.sort() which also uses Merge sort. Collections sort uses Arrays sort implementation underneath. So, in simple sense i can say that primitives are sorted using quick sort but objects are sorted using Merge sort.
My guess is it has something to do with sorting algorithm it self. There are so many discussion on SO on Quick sort vs Merge sort, like this and this. Seems to be there are conflicting claims on which one is better, which is understandable as this depend on data sets.
My understanding is
- In place: Quick sort wins. Merge sort can be implemented in in-place for Linked list
- External Storage Data: Merge sort wins.
- Sorting List (backed by any form of linked list): Merge sort wins. Link
Android API seems to follow the same pattern as Java. This is what i found in Arrays.java
public static void sort(long[] array) {
DualPivotQuicksort.sort(array);
}
And this,
public static void sort(Object[] array) {
ComparableTimSort.sort(array);
}
What i do not understand is, what makes Merge sort a good candidate for sorting objects in Java or in Android? Why not leaving this decision upto developers?