java快速排序
快速排序简介
快速排序(英文名:Quicksort,有时候也叫做划分交换排序)是一个高效的排序算法,由Tony Hoare在1959年发明(1961年公布)。当情况良好时,它可以比主要竞争对手的归并排序和堆排序快上大约两三倍。这是一个分治算法,而且它就在原地排序。
所谓原地排序,就是指在原来的数据区域内进行重排,就像插入排序一般。而归并排序就不一样,它需要额外的空间来进行归并排序操作。为了在线性时间与空间内归并,它不能在线性时间内实现就地排序,原地排序对它来说并不足够。而快速排序的优点就在于它是原地的,也就是说,它很节省内存。
手写
public static void Quicksort(int[] arr, int left, int right) {
if (left >= right){
return;
}
//将数组的最左边确定为基数
int base = arr[left];
//指定一个指针,从最右边向左边移,遇到小于基数的则暂停
int hight = right;
//指定一个指针,从最左边向右边移动,遇到大于基数的则暂停
int low = left;
//只要最右边和最左边的指针指向的不是同一个值
while (hight != low){
//上面这句话也可以理解为: 向左移在大于等于基数则不停止(继续左移 )
while (arr[hight] >= base && hight > low){
hight--;
}
//出了循环意味着向左移动的指针停止了, 找到了比基数小的值
//现在开始移动左边的指针向右边移动
//和上边同理
while (arr[low] <= base && low < hight){
low++;
}
//经过两个while 当前的状态就是两个指针都停止了, 此时交换两个指针所指的数.
int temp = arr[low];
arr[low] = arr[hight];
arr[hight] = temp;
//继续进行循环即可
}
//出循环意味着low == hight
//此时需要将base和 两个指针(low和hight)共同指的值进行交换
// 等同于 arr[left] = arr[hight]
arr[left] = arr[low];
arr[low] = base;
//现在已经进行了一次交换,base左边的全小于base,右边全大于base
//对左边再进行上述方法
//low 一定要 -1 因为中间是基数, 不要去动他, 所以指针向前移动一位
Quicksort(arr, left, low-1);
//同理 对右边也进行递归
//hight 一定要 +1
Quicksort(arr, hight + 1, arr.length-1);
}
Arrays.sort()—双轴快排
正当以为自己写了快排很开心时,顺便看了一下Arrays.sort()方法。
挖槽~~~~!!! 好快啊.
public static void main(String[] args){
//当前时间毫秒值
long statrTime = System.currentTimeMillis();
Random random = new Random();
int[] arr = new int[100000000];
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(1000);
}
/*Quicksort(arr,0, arr.length-1);
for (int a : arr) {
System.out.print(a + " ");
}
long endTime = System.currentTimeMillis();
System.out.println();
System.out.println((endTime - statrTime)/1000 + " s" );*/
//Arrays 中的排序
long statrTime2 = System.currentTimeMillis();
Arrays.sort(arr);
long endTime2 = System.currentTimeMillis();
System.out.println((endTime2 - statrTime2)/1000 + " s" );
}
//运行结果 5s
一亿 —5秒(如果你们的比我快,一定是我的笔记本老了,不行了),好快!!!
快去看看源码。
/**
* 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);
}
/**
* Sorts the specified range of the array using the given
* workspace array slice if possible for merging
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
* @param work a workspace array (slice)
* @param workBase origin of usable space in work array
* @param workLen usable size of work array
*/
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;
}
}
在这个方法中有个说明,如果是小数组就直接使用快速排序,而这个小是指数组长度小于 286
// Use Quicksort on small arrays
if (right - left < QUICKSORT_THRESHOLD) {
sort(a, left, right, true);
return;
}
/**
* If the length of an array to be sorted is less than this
* constant, Quicksort is used in preference to merge sort.
*/
private static final int QUICKSORT_THRESHOLD = 286;
至于为什么是286来个大佬能不能解释一下。
当然还有其他排序推荐的阈值
/*
* Tuning parameters.
*/
/**
* The maximum number of runs in merge sort.
* 归并排序中的最大运行次数。
*/
private static final int MAX_RUN_COUNT = 67;
/**
* The maximum length of run in merge sort.
* 归并排序中运行的最大长度。
*/
private static final int MAX_RUN_LENGTH = 33;
/**
* If the length of an array to be sorted is less than this
* 如果要排序的数组的长度小于这个
* constant, Quicksort is used in preference to merge sort.
* 常量,快速排序优先用于合并排序
*/
private static final int QUICKSORT_THRESHOLD = 286;
/**
* If the length of an array to be sorted is less than this
* constant, insertion sort is used in preference to Quicksort.
* 插入排序优先于快速排序。
*/
private static final int INSERTION_SORT_THRESHOLD = 47;
/**
* If the length of a byte array to be sorted is greater than this
* constant, counting sort is used in preference to insertion sort.
* 计数排序优先于插入排序。
*/
private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29;
/**
* If the length of a short or char array to be sorted is greater
* 如果要排序的短数组或字符数组的长度较大
* than this constant, counting sort is used in preference to Quicksort.
* 与此常量相比,计数排序优先于快速排序。
*/
private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;
总结
Arrays.sort()主要是通过数组的情况不同选择不同的排序方式,比如:数组有点顺序就选择归并排序。没有最好的算法,只有更适合的方法。