Arrays.sort如何实现降序排序

目录
  • Arrays.sort实现降序排序
    • 1.Collections的reverseOrder
    • 2.利用Comparator接口复写compare
  • Arrays.sort底层原理
    • 概述
    • 案例
  • 总结

Arrays.sort实现降序排序

在调用Arrays.sort()对数组进行排序时,默认是升序排序的,如果想让数组降序排序,有下面两种方法:

1.Collections的reverseOrder

import java.util.*;
 
public class Main {
    public static void main(String[] args) {
//        注意这里是Integer,不是int
        Integer[] arr={9,8,7,6,5,4,3,2,1};
        Arrays.sort(arr,Collections.reverseOrder());
        for(int i:arr){
            System.out.println(i);
        }
    }
}

2.利用Comparator接口复写compare

import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Integer[] arr={9,8,7,6,5,4,3,2,1};
        Comparator cmp=new CMP();
        Arrays.sort(arr,cmp);
        for(int i:arr){
            System.out.println(i);
        }
    }
}
class CMP implements Comparator<Integer>{
    @Override //可以去掉。作用是检查下面的方法名是不是父类中所有的
    public int compare(Integer a,Integer b){
//        两种都可以,升序排序的话反过来就行
//        return a-b<0?1:-1;
        return b-a;
    }
}

注意:如果需要改变默认的排列方式,不能使用基本类型(int,char等)定义变量,而应该用对应的类

Arrays.sort底层原理

概述

Collections.sort()方法底层调用的也是Arrays.sort()方法,下面我们通过测试用例debug,探究一下其源码,首先说一下结果,使用到了插入排序,双轴快排,归并排序

双轴快排(DualPivotQuicksort): 顾名思义有两个轴元素pivot1,pivot2,且pivot ≤
pivot2,将序列分成三段:x < pivot1、pivot1 ≤ x ≤ pivot2、x >pivot2,然后分别对三段进行递归。这个算法通常会比传统的快排效率更高,也因此被作为Arrays.java中给基本类型的数据排序的具体实现。

大致流程:

快速排序部分展开

案例

	public static void main(String[] args) {
        int[] nums = new int[]{6,5,4,3,2,1};
        List<Integer> list = Arrays.asList(6, 5, 4, 3, 2, 1);
        Arrays.sort(nums);
        Collections.sort(list);
        System.out.println(Arrays.toString(nums));
        System.out.println(list);

    }

运行结果

1 进入Arrays.sort()方法

/**
     * Sorts the specified array into ascending numerical order.
     *
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
     * 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.
     *
     * @param a the array to be sorted
     */
    public static void sort(int[] a) {
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
    }

方法上的注释

2 进入DualPivotQuicksort类内部的静态方法sort

方法上的注释

3 走sort的流程

1. 排序范围小于286的数组使用快速排序

 	// Use Quicksort on small arrays
    if (right - left < QUICKSORT_THRESHOLD) {
            sort(a, left, right, true);
            return;
    }
    // Merge sort
    ......

2. 进入sort方法,判断数组长度是否小于47,小于则直接采用插入排序,否则执行3。

	 // Use insertion sort on tiny arrays
    if (length < INSERTION_SORT_THRESHOLD) {
	   // Insertion sort
	   ......
    }

3. 用公式length/8+length/64+1近似计算出数组长度的1/7。

// Inexpensive approximation of length / 7
int seventh = (length >> 3) + (length >> 6) + 1;

4. 取5个根据经验得出的等距点。

		/*
         * Sort five evenly spaced elements around (and including) the
         * center element in the range. These elements will be used for
         * pivot selection as described below. The choice for spacing
         * these elements was empirically determined to work well on
         * a wide variety of inputs.
         */
        int e3 = (left + right) >>> 1; // The midpoint
        int e2 = e3 - seventh;
        int e1 = e2 - seventh;
        int e4 = e3 + seventh;
        int e5 = e4 + seventh;

5.将这5个元素进行插入排序

		// Sort these elements using insertion sort
        if (a[e2] < a[e1]) { long t = a[e2]; a[e2] = a[e1]; a[e1] = t; }

        if (a[e3] < a[e2]) { long t = a[e3]; a[e3] = a[e2]; a[e2] = t;
            if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
        }
        if (a[e4] < a[e3]) { long t = a[e4]; a[e4] = a[e3]; a[e3] = t;
            if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
                if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
            }
        }
        if (a[e5] < a[e4]) { long t = a[e5]; a[e5] = a[e4]; a[e4] = t;
            if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
                if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
                    if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
                }
            }
        }

6. 选取a[e2],a[e4]分别作为pivot1,pivot2。由于步骤5进行了排序,所以必有pivot1 <=pivot2。定义两个指针less和great,less从最左边开始向右遍历,一直找到第一个不小于pivot1的元素,great从右边开始向左遍历,一直找到第一个不大于pivot2的元素。

		 /*
         * Use the second and fourth of the five sorted elements as pivots.
         * These values are inexpensive approximations of the first and
         * second terciles of the array. Note that pivot1 <= pivot2.
         */
        int pivot1 = a[e2];
        int pivot2 = a[e4];
        /*
         * The first and the last elements to be sorted are moved to the
         * locations formerly occupied by the pivots. When partitioning
         * is complete, the pivots are swapped back into their final
         * positions, and excluded from subsequent sorting.
         */
        a[e2] = a[left];
        a[e4] = a[right];
        /*
         * Skip elements, which are less or greater than pivot values.
         */
        while (a[++less] < pivot1);
        while (a[--great] > pivot2);

7. 接着定义指针k从less-1开始向右遍历至great,把小于pivot1的元素移动到less左边,大于pivot2的元素移动到great右边。这里要注意,我们已知great处的元素小于pivot2,但是它于pivot1的大小关系,还需要进行判断,如果比pivot1还小,需要移动到到less左边,否则只需要交换到k处。

			/*
             * Partitioning:
             *
             *   left part           center part                   right part
             * +--------------------------------------------------------------+
             * |  < pivot1  |  pivot1 <= && <= pivot2  |    ?    |  > pivot2  |
             * +--------------------------------------------------------------+
             *               ^                          ^       ^
             *               |                          |       |
             *              less                        k     great
             *
             * Invariants:
             *
             *              all in (left, less)   < pivot1
             *    pivot1 <= all in [less, k)     <= pivot2
             *              all in (great, right) > pivot2
             *
             * Pointer k is the first index of ?-part.
             */
            outer:
            for (int k = less - 1; ++k <= great; ) {
                short ak = a[k];
                if (ak < pivot1) { // Move a[k] to left part
                    a[k] = a[less];
                    /*
                     * Here and below we use "a[i] = b; i++;" instead
                     * of "a[i++] = b;" due to performance issue.
                     */
                    a[less] = ak;
                    ++less;
                } else if (ak > pivot2) { // Move a[k] to right part
                    while (a[great] > pivot2) {
                        if (great-- == k) {
                            break outer;
                        }
                    }
                    if (a[great] < pivot1) { // a[great] <= pivot2
                        a[k] = a[less];
                        a[less] = a[great];
                        ++less;
                    } else { // pivot1 <= a[great] <= pivot2
                        a[k] = a[great];
                    }
                    /*
                     * Here and below we use "a[i] = b; i--;" instead
                     * of "a[i--] = b;" due to performance issue.
                     */
                    a[great] = ak;
                    --great;
                }
            }

8. 将枢轴交换到它们的最终位置

	// Swap pivots into their final positions
    a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
    a[right] = a[great + 1]; a[great + 1] = pivot2;

9. 递归排序左右部分,不包括已知的枢轴

		// Sort left and right parts recursively, excluding known pivots
        sort(a, left, less - 2, leftmost);
        sort(a, great + 2, right, false);

10. 对于中间的部分,如果大于4/7的数组长度,递归中间部分

			/*
             * If center part is too large (comprises > 4/7 of the array),
             * swap internal pivot values to ends.
             */
            if (less < e1 && e5 < great) {
                /*
                 * Skip elements, which are equal to pivot values.
                 */
                while (a[less] == pivot1) {
                    ++less;
                }

                while (a[great] == pivot2) {
                    --great;
                }

                /*
                 * Partitioning:
                 *
                 *   left part         center part                  right part
                 * +----------------------------------------------------------+
                 * | == pivot1 |  pivot1 < && < pivot2  |    ?    | == pivot2 |
                 * +----------------------------------------------------------+
                 *              ^                        ^       ^
                 *              |                        |       |
                 *             less                      k     great
                 *
                 * Invariants:
                 *
                 *              all in (*,  less) == pivot1
                 *     pivot1 < all in [less,  k)  < pivot2
                 *              all in (great, *) == pivot2
                 *
                 * Pointer k is the first index of ?-part.
                 */
                outer:
                for (int k = less - 1; ++k <= great; ) {
                    short ak = a[k];
                    if (ak == pivot1) { // Move a[k] to left part
                        a[k] = a[less];
                        a[less] = ak;
                        ++less;
                    } else if (ak == pivot2) { // Move a[k] to right part
                        while (a[great] == pivot2) {
                            if (great-- == k) {
                                break outer;
                            }
                        }
                        if (a[great] == pivot1) { // a[great] < pivot2
                            a[k] = a[less];
                            /*
                             * Even though a[great] equals to pivot1, the
                             * assignment a[less] = pivot1 may be incorrect,
                             * if a[great] and pivot1 are floating-point zeros
                             * of different signs. Therefore in float and
                             * double sorting methods we have to use more
                             * accurate assignment a[less] = a[great].
                             */
                            a[less] = pivot1;
                            ++less;
                        } else { // pivot1 < a[great] < pivot2
                            a[k] = a[great];
                        }
                        a[great] = ak;
                        --great;
                    }
                }
            }

            // Sort center part recursively
            sort(a, less, great, false);

4 小结

Arrays.sort对升序数组、降序数组和重复数组的排序效率有了很大的提升,这里面有几个重大的优化。

  • 对于小数组来说,插入排序效率更高,每次递归到小于47的大小时,用插入排序代替快排,明显提升了性能。
  • 双轴快排使用两个pivot,每轮把数组分成3段,在没有明显增加比较次数的情况下巧妙地减少了递归次数。
  • pivot的选择上增加了随机性,却没有带来随机数的开销。
  • 对重复数据进行了优化处理,避免了不必要交换和递归。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Java Arrays.sort和Collections.sort排序实现原理解析

    1.使用 排序 2.原理 事实上Collections.sort方法底层就是调用的array.sort方法,而且不论是Collections.sort或者是Arrays.sort方法, 跟踪下源代码吧,首先我们写个demo public static void main(String[] args) { List<String> strings = Arrays.asList("6", "1", "3", "1",

  • Java的Arrays.sort()方法排序算法实例分析

    暂时网上看过很多JDK8中Arrays.sort的底层原理,有些说是插入排序,有些说是归并排序,也有说大于域值用计数排序法,否则就使用插入排序...其实不全对.让我们分析个究竟: // Use Quicksort on small arrays if (right - left < QUICKSORT_THRESHOLD) { //QUICKSORT_THRESHOLD = 286 sort(a, left, right, true); return; } 数组一进来,会碰到第一个阀值QUICK

  • Arrays.sort(arr)是什么排序及代码逻辑

    在学习过程中观察到Arrays.sort(arr)算法可以直接进行排序,但不清楚底层的代码逻辑是什么样子,记得自己之前在面试题里面也有面试官问这个问题,只能说研究之后发现还是比较复杂的,并不是网上说的快排或者二分插入之类的. 首先看源码: public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); } 它调用了DualPivotQuicksort的sort方法,乍一看以为是

  • Java使用Arrays.sort()方法实现给对象排序

    目录 使用Arrays.sort()方法给对象排序 麻烦的方法 Arrays.sort()方法 浅谈Arrays.sort()原理 例子1 基础知识点 例子2 双轴快排 另外参考了其他博文,算法思路如下 使用Arrays.sort()方法给对象排序 当我们给一个整型数组或者浮点型之类的数组排序的时候,很简单就可以达到我们排序的目的,无非是排序算法的问题.那么,如果我们现在想根据对象的一个属性值给一个对象数组进行排序呢? 假如我们现在有一个Car类型,Car类中有一个double型的speed属性

  • Java使用lambda自定义Arrays.sort排序规则说明

    目录 lambda自定义Arrays.sort排序规则 1.类间排序 2.使用比较器(comparator)作为sort的参数(用于单个类型的排序) 补充一下 Arrays.sort()的一些用法 1.对指定T型数组按指定数值升序排序 2.对指定T型数组的指定范围按指定数值升序排序 3.根据指定比较器产生的顺序对指定对象数组进行排序 lambda自定义Arrays.sort排序规则 1.类间排序 首先注意默认排规则,当使用sort(Objetc[] a)来进行对象的自然排序,该对象必需实现Com

  • Arrays.sort如何实现降序排序

    目录 Arrays.sort实现降序排序 1.Collections的reverseOrder 2.利用Comparator接口复写compare Arrays.sort底层原理 概述 案例 总结 Arrays.sort实现降序排序 在调用Arrays.sort()对数组进行排序时,默认是升序排序的,如果想让数组降序排序,有下面两种方法: 1.Collections的reverseOrder import java.util.*;   public class Main {     public

  • 利用stream sorted进行降序排序

    根据value值的大小进行降序排序,并进行截取. public static void main(String[] args) { List<Map<String, Object>> list = Lists.newArrayList(); Map<String, Object> map = Maps.newHashMap(); map.put("id", 1); map.put("value", 20); list.add(ma

  • 如何使用Collections.reverse对list集合进行降序排序

    目录 使用Collections.reverse对list集合进行降序排序 Collections.reverse原理 使用Collections.reverse对list集合进行降序排序 今天无意中搜了一下Collections.reverse这个方法,结果发现有些人对它的误解蛮深的.下面是一个有百万访问量博主写的,reverse可以对指定列表进行降序排序,可是自己输出的结果都不是降序. 确实,使用Collections.reverse结合一定方法可以实现对list集合降序排序,但是直接使用C

  • DataGridView实现点击列头升序和降序排序

    DataGridView 列有三种排序模式.每一列的排序模式是通过该列的 SortMode 属性指定的,该属性可以设置为以下的 DataGridViewColumnSortMode 枚举值之一. DataGridViewColumnSortMode 值说明: Automatic文本框列的默认排序模式.除非将列标头用于选择,否则单击列标头将自动按此列对 DataGridView 排序,并显示一个指示排序顺序的标志符号(向上的三角箭头:升序排序:向下的三角箭头:降序排序). NotSortable非

  • php arsort 数组降序排序详细介绍

    arsort 对数组进行降序排序并保持索引关系. 基本语法 bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) 本函数对数组进行降序排序,数组的索引保持和单元的关联. arsort函数主要用于对那些单元顺序很重要的结合数组进行排序. 参数介绍: 参数 描述 array 必需.输入的数组. sort_flags 可选.规定如何排列数组的元素/项目.可能的值: 0 = SORT_REGULAR - 默认.把每一项按常规顺

  • php数组函数序列之rsort() - 对数组的元素值进行降序排序

    rsort()定义和用法 rsort() 函数对数组的元素按照键值进行逆向排序.与 arsort() 的功能基本相同. 注释:该函数为 array 中的单元赋予新的键名.这将删除原有的键名而不仅是重新排序. 如果成功则返回 TRUE,否则返回 FALSE. 可选的第二个参数包含另外的排序标志. 语法 rsort(array,sorttype)参数 描述 array 必需.输入的数组. sorttype 可选.规定如何排列数组的值.可能的值: SORT_REGULAR - 默认.以它们原来的类型进

  • php数组函数序列之krsort()- 对数组的元素键名进行降序排序,保持索引关系

    krsort()定义和用法 krsort() 函数将数组按照键逆向排序,为数组值保留原来的键. 可选的第二个参数包含附加的排序标志. 若成功,则返回 TRUE,否则返回 FALSE. 语法 krsort(array,sorttype)参数 描述 array 必需.规定要排序的数组. sorttype 可选.规定如何排列数组的值.可能的值: SORT_REGULAR - 默认.以它们原来的类型进行处理(不改变类型). SORT_NUMERIC - 把值作为数字来处理 SORT_STRING - 把

  • vuejs通过filterBy、orderBy实现搜索筛选、降序排序数据

    直接贴代码了: 先上输入前的样子: <style> #example{margin:100px auto;width:600px;} .show{margin:10px;} #searchText{display: block;margin:0 auto 10px;height:24px;line-height: 24px;width:200px;} .content ul li{text-align: center;} .content ul li span{display: inline-

  • C++ sort排序之降序、升序使用总结

    一.升序 C++ sort 函数十分方便,可以对内置类型也可对自定义类型进行快速排序,内置类型的使用比较简单,下面主要讨论自定义类型的排序,一般有如下几种使用方法: 1.1 重载比较操作符 比如,我们现有一批学生,要根据他们的成绩进行升序排序,成绩如果相等则根据名字升序排序,那么我们可以如下操作: struct Student{ string name; int grade; Student(string name, int grade) : name(name), grade(grade){}

随机推荐