Common method comparison - c++ & java
Sort
C++
For c++, here is official document
sort()
This method sorts elements in the range [first, last).
Result is in ascending order by deflaut.
Introsort.
|
|
On average, linearithmic in the distance between first and last: Performs approximately N*log2N
stable_sort()
sort()
is unstable. However, stable_sort()
preserves the relative order of the elements with equivalent value.
If enough extra memory is available, linearithmic in the distance between first and last: Performs up to N*log2(N) element comparisons (where N is this distance), and up to that many element moves.
Otherwise, polyloglinear in that distance: Performs up to N*log22(N) element comparisons, and up to that many element swaps.
partial_sort()
partial_sort()
sort first several elements while the remaining elements are left without any specific order.
|
|
Java
sort(), primitive type and object type. parallelSort().collection.sort()
Array.sort()
Package: java.util.Arrays;
Sorts the specified array into ascending numerical order.
For primitive type, The sorting algorithm is a Dual-Pivot Quicksort(Orignal source) by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations. This quicksort is unstable.
For object type, the sorting algorithm is Timsort which is combined character of merge sort and selection sort(wiki). This is a stable sort algorithm.
Primitive type
public static void sort(int[] a);
|
|
Object type
public static void sort(Object[] a)
public static void sort(Object[] a, int fromIndex, int toIndex)
public static <T> void sort(T[] a, Comparator<? super T> c)
public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
Arrays.parallelSort()
This is a stable sort algorithm.
Only if the size of array is larger than MIN_ARRAY_SORT_GRAN, algorithm can use multi-thread. (MIN_ARRAY_SORT_GRAN is 1«13, that is 8192, in source code.)
*The sorting algorithm is a parallel sort-merge that breaks the array into sub-arrays that are themselves sorted and then merged.(JavaDoc)
Container
|
|