Arrays.sort()源码分析

6版本以下用插入和归并判断的
7版本object的排序依然用的归并排序,int和其他基本类型用的改进的插入排序双轴快速排序和归并排序,但归并排序已经被注释即将过时.
java8以上数组元素个数<47用的插入排序,大于47小于286用的双轴快速插入,大于286后做判断,判断原则是以已经分成若干子区域的数组的无序程度,如果无序程度大于67,依然使用双轴快速排序,小于67使用归并排序.
First, Arrays.sort(int[] a) analysis
Summarize the Arrays.sort(int[] a) method:

  1. Array length 0-47: Strip
  2. Array length 47-286: Dual-Pivot fast row
  3. Array length >286
  4. The array is basically ordered: merge
  5. 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:
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值