Why is Merge sort used for objects in the Android / Java API?

In Java, Arrays.sort () uses a quick sort for a primitive type. Arrays.sort (), on the other hand, uses Merge sorting for objects. And the same goes for Collection.sort () , which also uses Merge sorting. To sort the collection, sort sorting of arrays underneath is used. So, in a simple sense, I can say that primitives are sorted using quick sort, but objects are sorted using Merge sort.

I guess this has something to do with the sorting algorithm. There is so much discussion of SO on Quick sort vs Merge sort, like this and. It seems that there are conflicting statements for which it is better, which is understandable, since it depends on the data sets.

My understanding

  • In place: Quick sorting. Merge sorting can be implemented in place for the Linked List
  • External storage data: merge sort wins.
  • Sort list (supported by any form of linked list): sorting wins by volume. Link

The 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);
}

, Merge Java Android? ?

+4
4

- , , .

, . long. 3 , , .

, , . , , "" "" .

Arrays.sort mergesort . , , .

+7

, : Collections.sort quicksort?

TL; :

QuickSort mergesort:

  • ( ).
  • n log n; .
+2

,

Java ?

,

?

.

, Collection ArrayList sort() ( sort() - static, be , ) .

, ( -) , , , Java, ++. Java "" , , , , , . JVM /. , .

++. :

Actually, I do not think that a library can eliminate the need for a programmer to know algorithms and data structures. This only eliminates the need for programming to implement them. You need to understand the fundamental properties of data structures in order to use them correctly so that the application meets its complexity requirements.

+2
source

Merge sort is stable: equal elements will not be reordered as a result of sorting

+1
source

All Articles