千家信息网

Arrays.sort(arr)代码逻辑是什么

发表于:2024-10-23 作者:千家信息网编辑
千家信息网最后更新 2024年10月23日,本篇内容介绍了"Arrays.sort(arr)代码逻辑是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学
千家信息网最后更新 2024年10月23日Arrays.sort(arr)代码逻辑是什么

本篇内容介绍了"Arrays.sort(arr)代码逻辑是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

首先看源码:

 public static void sort(int[] a) {        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);    }

它调用了DualPivotQuicksort的sort方法,乍一看以为是快排,这是很多网上的博主的说法,继续点开向下看(代码太长,没耐心看可以直接跳过该段代码QWQ):

 static void sort(int[] a, int left, int right,                     int[] work, int workBase, int workLen) {        // Use Quicksort on small arrays        if (right - left < QUICKSORT_THRESHOLD) {            sort(a, left, right, true);            return;        }        /*         * Index run[i] is the start of i-th run         * (ascending or descending sequence).         */        int[] run = new int[MAX_RUN_COUNT + 1];        int count = 0; run[0] = left;        // Check if the array is nearly sorted        for (int k = left; k < right; run[count] = k) {            if (a[k] < a[k + 1]) { // ascending                while (++k <= right && a[k - 1] <= a[k]);            } else if (a[k] > a[k + 1]) { // descending                while (++k <= right && a[k - 1] >= a[k]);                for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {                    int t = a[lo]; a[lo] = a[hi]; a[hi] = t;                }            } else { // equal                for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {                    if (--m == 0) {                        sort(a, left, right, true);                        return;                    }                }            }            /*             * The array is not highly structured,             * use Quicksort instead of merge sort.             */            if (++count == MAX_RUN_COUNT) {                sort(a, left, right, true);                return;            }        }        // Check special cases        // Implementation note: variable "right" is increased by 1.        if (run[count] == right++) { // The last run contains one element            run[++count] = right;        } else if (count == 1) { // The array is already sorted            return;        }        // Determine alternation base for merge        byte odd = 0;        for (int n = 1; (n <<= 1) < count; odd ^= 1);        // Use or create temporary array b for merging        int[] b;                 // temp array; alternates with a        int ao, bo;              // array offsets from 'left'        int blen = right - left; // space needed for b        if (work == null || workLen < blen || workBase + blen > work.length) {            work = new int[blen];            workBase = 0;        }        if (odd == 0) {            System.arraycopy(a, left, work, workBase, blen);            b = a;            bo = 0;            a = work;            ao = workBase - left;        } else {            b = work;            ao = 0;            bo = workBase - left;        }        // Merging        for (int last; count > 1; count = last) {            for (int k = (last = 0) + 2; k <= count; k += 2) {                int hi = run[k], mi = run[k - 1];                for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {                    if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {                        b[i + bo] = a[p++ + ao];                    } else {                        b[i + bo] = a[q++ + ao];                    }                }                run[++last] = hi;            }            if ((count & 1) != 0) {                for (int i = right, lo = run[count - 1]; --i >= lo;                    b[i + bo] = a[i + ao]                );                run[++last] = right;            }            int[] t = a; a = b; b = t;            int o = ao; ao = bo; bo = o;        }    }

代码很长,简要翻译过来,这里分了好几种情况:

1.数组长度小于286

这里又会调用一个sort方法,点开该sort(),又会划分情况:

数组长度小于47,

当leftmost(导入的一个布尔参数)为true,则使用直接插入排序;

否则会调用另一种插入办法,这里可以观察到一个注释:

/*
* Every element from adjoining part plays the role
* of sentinel, therefore this allows us to avoid the
* left range check on each iteration. Moreover, we use
* the more optimized algorithm, so called pair insertion
* sort, which is faster (in the context of Quicksort)
* than traditional implementation of insertion sort.
*/

大致意思是:相邻部分的每个元素都扮演着哨兵的角色,因此这允许我们避免在每次迭代中进行左范围检查。此外,我们使用了更优化的算法,即所谓的成对插入排序,它比插入排序的传统实现更快(在快速排序的上下文中)。

不过注意到,原函数参数传参在这里leftmost为true,所以一定是直接插入排序,以上作为了解。

至于大过INSERTION_SORT_THRESHOLD(47)的,用一种快速排序的方法:

1.从数列中挑出五个元素,称为 "基准"(pivot);

2.重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;

3.递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。

当数组长度大于286时

此时回到那段很长很长的代码段,在判断小于286的长度数组之后,从注解中:

// Check if the array is nearly sorted

这里是指检查数组元素是不是快要排列好了,或者书面一点说,是不是有一定结构了,然后看后面的for循环,注意到一段代码:

 if (++count == MAX_RUN_COUNT) {                sort(a, left, right, true);                return;            }

这里的sort和我们上面在数组长度小于286时的那个sort方法是同一个方法,而针对这个count,是用来记录逆序组的,打个比方:

此时有一个数组为1 5 6 9 8 7 2 3

当数组认定我们的顺序应该为升序时,从第一个数开始数,此时9 8 7 2为降序,这就是逆序,将这四个数组合成一个组称为逆序组,然后再从3开始往后看。

当统计到一个逆序组时,count++,所以可以看出,count是用来记逆序组的,那么逆序组越多,这个结构就越混乱

MAX_RUN_COUNT == 67 ,因此当count一直加到67时,就说明已经在一个混乱的临界值了,此时执行sort()方法

通过这一段分析,我们理一下思路:

如果数组能运行到这里,说明数组的长度大于等于286。符合该条件时,我们要判断这个数组是否有一定的结构:

(1)count<67,说明不是那么混乱,有一定结构,跳过;

(2)count>=67,说明已经混乱了,没有结构,执行sort方法,而已知数组长度大于等于286,那么必然大于47,一定执行快速排序。

跳过之后,经过代码的一大堆前置操作,最后看见下面的代码里面一行注释:

//Merging

显然,这里后面用到归并排序了,不详细赘述。

好了,最后总结:

  • 数组长度小于286时,根据数组长度,分两种情况:

    • 数组长度小于47,使用直接插入排序

    • 数组长度大于47,使用快速排序

  • 数组长度大于286时,根据数组排序情况,分两种情况:

    • 数组内顺序较为混乱,即count逆序组数大于等于67,使用快速排序

    • 数组内有一定顺序,即count逆序组数小于67,使用归并排序

"Arrays.sort(arr)代码逻辑是什么"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0