Java uses a quick sort of array of Primitive(int,float , and other prototype data) and merges the object array with a merge sort.
??
Because the optimized merge sort is fast (nlog (n)) and stable.
??
For the ordering of objects, stability is important . such as the report card, the beginning may be according to the number of people in the order of the line, and now let us use scores platoon, then you should ensure that the original Zhang San in front of John Doe, even if their results are the same, Zhang San can not run to the back of John Doe.
??
The fast sort is unstable, and the worst-case time complexity is O (n^2).
??
In addition, the object array is only a reference to the object, so multiple shifts do not cause additional overhead, but the object array is generally more sensitive to the comparison, it is possible that the comparison of the object is much more expensive than the simple number. Merge sort is better than fast sorting in this respect, which is one of the important reasons to choose it as an object sort.
??
Sorting optimization: The implementation of the fast and merge are recursive, and at the bottom of the recursion, that is, to sort the array length is less than 7 , the direct use of bubble sort, and no longer recursive .
??
Analysis: An array of length 6 bubble sort the total number of comparisons is 1+2+3+4+5+6=21 times, preferably only 6 times compared. The overhead of a quick row or merge involves recursive invocation, and its time efficiency is highlighted at the lesser of N , so a bubbling sort is used, which is an extremely important optimization for fast sequencing.
??
The rapid sequencing in the source code, mainly to do the following several aspects of optimization:
??
1) when the number of elements in the array to be sorted is small, the threshold value in the source code is 7, and the insertion sort is used. While insertion sorting has a time complexity of 0 (n^2), insertion sorting is preferable to fast sorting when there are fewer array elements, because recursive operations that quickly sort affect performance.
??
2) better selection of the partition element (datum element). The ability to divide an array into roughly two equal parts avoids the worst case scenario. For example, when the array is ordered, selecting the first element as the partition element will make the time complexity of the algorithm reach O (n^2).
??
The method of selecting the partition element in the source code :
??
When the array size is size=7 , the middle element of the array is taken as the dividing element. int n=m>>1; ( This method is worth learning )
??
When the size of the array is 7<size<=40 , the elements of the middle size of the first, middle, and last three elements are taken as the dividing element.
??
When the size of the array is size>40, select 9 elements from the array to be spaced evenly, and choose a pseudo-median as the dividing element.
??
3) According to the division of the v , the formation of the invariant v* (<v) * (>v) * v*
??
The ordinary fast sorting algorithm, after one partition, divides the element into the middle position of the group, the elements on the left is less than the dividing element, the right element is greater than the dividing element, and the elements equal to the dividing element are not placed near it, this point, in arrays.sort () has been greatly optimized.
??
Examples:----6,7, A.
??
Because of 7<size<=40, so in the6, and then select v = The partition of the element.
??
After abreak:7,6, three, five, 15, , and. elements equal to the dividing element are moved to both sides of the group .
??
Next, the elements equal to the partition are moved to the middle of the array to form:7,6, A. a.
??
Finally, the two intervals are sorted by recursion [7,6] and [x,x,x].
??
Http://www.cnblogs.com/gw811/archive/2012/10/04/2711746.html
Array.Sort Source