Arrays.sort()用的是什么排序算法?怎么优化?

来源:互联网 发布:汽车行业制图软件 编辑:程序博客网 时间:2024/05/16 08:05

Arrays.sort()用的是快速排序算法。相信大家对于这个都是了解的。
算法的思想:
选择基准将数组一分为二,基准前面的比基准小,基准后面的比基准大,之后分别对这两部分继续之前的操作,已达到整个数组有序的目的。
算法内容描述:
先选择一个基准,指向数组开始的指针start和指向数组结束的指针end;
当start小于end的时候,如果基准的值小于end指向数组的值时,end往前移动;
当基准的值不在小于end指向数组的值的时候,交换两个指针指向的数组的值;
然后当基准的值大于start指向数组的值的时候,start往后移动;
当基准的值不大于start指向数组的值的时候,交换两个指针指向的数组的值;
返回基准的位置并进行递归操作完成排序。

代码如下:

public class Test2 {    public static void swap(int[] arr, int j, int i){        int temp = arr[j];        arr[j] = arr[i];        arr[i] = temp;    }    public static int partition(int arr[], int start, int end){        assert(null != arr);        int temp = arr[start];        while(start < end){            while(temp < arr[end] && start < end){                end--;            }            swap(arr, start, end);            while(temp > arr[start] && start < end){                start++;            }            swap(arr, start, end);        }        System.out.println(Arrays.toString(arr) + "   " +  start);        return start;    }    public static void partitionSort(int arr[], int start, int end){        assert(null != arr);        if(start < end){            int midd = partition(arr, start, end);            partitionSort(arr, start, midd - 1);            partitionSort(arr, midd + 1, end);        }    }    public static void main(String[] args) {        int arr[] = {9,1,5,8,3,7,4,6,2};          Test2.partitionSort(arr, 0, arr.length - 1);        System.out.println(Arrays.toString(arr));    }}

执行结果:
[2, 1, 5, 8, 3, 7, 4, 6, 9] 8
[1, 2, 5, 8, 3, 7, 4, 6, 9] 1
[1, 2, 4, 3, 5, 7, 8, 6, 9] 4
[1, 2, 3, 4, 5, 7, 8, 6, 9] 3
[1, 2, 3, 4, 5, 6, 7, 8, 9] 6
[1, 2, 3, 4, 5, 6, 7, 8, 9]

快速排序的优化:
①优化基准的选择
上面的程序选择的基准是数组起始位置,但是跟明显,我们并没有达到想要的理想结果将数组划分为两部分,进行递归操作;
所以就有了三数取中法来选取基准,即取三个关键字先进行排序,将中间数作为基准,一般去开始,结束,和中间;
当然也可以随机选取;其实还有一种九数取中法,这里就不详细介绍了,有兴趣的可以自己了解一下。
下面是三数取中法的代码:

public void medianOfThree(int[] arr, int start, int end){        int m = start + (end - start) / 2;        if(arr[start] > arr[end]){            swap(arr, start, end);        }        if(arr[m] > arr[end]){            swap(arr, end, m);        }        if(arr[m] > arr[start]){            swap(arr, m, start);        }    }

②优化不必要的交换
首先我们通过上面的代码很容易发现在交换的过程中,有许多部分是没必要交换的,于是我们通过赋值替代交换来省去没必要的交换;

代码如下:

public int partition3(int arr[], int start, int end){        assert(null != arr);        medianOfThree(arr, start, end);        int temp = arr[start];        while(start < end){            while(temp < arr[end] && start < end){                end--;            }            arr[start] = arr[end];            while(temp > arr[start] && start < end){                start++;            }            arr[end] = arr[start];        }        arr[start] = temp;        System.out.println(Arrays.toString(arr) + "   " +  start);        return start;    }

③优化小数组时的排序方案
一般对于小数组排序,我们需要选择插入排序,因为插入排序是简单排序性能最高的。所以我们做如下修改:

public void partitionSort4(int arr[], int start, int end){        assert(null != arr);        if((end - start) > INSERT_SORT){            int midd = partition3(arr, start, end);            partitionSort3(arr, start, midd - 1);            partitionSort3(arr, midd + 1, end);        }else{            insertSort(arr);        }    }

其中,INSERT_SORT选择的大小众说纷纭,自我觉得在海量数据面前,选择20跟选择7没有太大的差异吧。(这话如果有误,望大家批评指正)
④优化递归操作
我们都知道递归的额外花销还是很大的,减少递归可以大大提高性能,故此做如下修改:

 public void partitionSort5(int arr[], int start, int end){        assert(null != arr);        int midd;        if((end - start) > INSERT_SORT){            while(start < end){                midd = partition3(arr, start, end);                partitionSort5(arr, start, midd - 1);                start = midd + 1;            }        }else{            insertSort(arr);        }    }

以上全是本人自己对于快速排序的总结,如果有什么不恰当或者错误的地方,还望各位批评指正!