6版本以下用插入和归并判断的
7版本object的排序依然用的归并排序,int和其他基本类型用的改进的插入排序双轴快速排序和归并排序,但归并排序已经被注释即将过时.
java8以上数组元素个数<47用的插入排序,大于47小于286用的双轴快速插入,大于286后做判断,判断原则是以已经分成若干子区域的数组的无序程度,如果无序程度大于67,依然使用双轴快速排序,小于67使用归并排序.
First, Arrays.sort(int[] a) analysis
Summarize the Arrays.sort(int[] a) method:
- Array length 0-47: Strip
- Array length 47-286: Dual-Pivot fast row
- Array length >286
- The array is basically ordered: merge
- Array out of order: Dual-Pivot fast row
Arrays.sort has a number of overloaded methods. Here we look at the Arrays.sort(int[] a) method. Other methods are similar.
Public static void sort(int[] a) {
DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}
You can see that the Sort method of the DualPivotQuicksort class is called. The DualPivotQuicksort is a quick sort. The students who want to know can read these two articles and explain them very thoroughly:
Two-axis fast sorting - implementation principle
Why is DualPivotQuicksort better than regular fast row?
Let’s take a look at the DualPivotQuicksort.sort method:
Static void sort(int[] a, int left, int right,
Int[] work, int workBase, int workLen) {
/**
* When the array length to be sorted is less than QUICKSORT_THRESHOLD=286: