Java Arrays.sort部分源码分析(1)

Java中的Arrays.sort提供了对Java所有类型的排序,其中有基本类型(8种)和Object类型。
    基本类型:快速排序
    Object类型:归并排序
    但是sort的排序不止这么两种,还有其他的排序在里面比如有(插入排序(优化过的))
 
因为对象的排序要求是稳定,所以不能使用快速排序,而且快速排序的最坏的时间复杂度是O(n^2),而优化后的归并排序是 nlog(n)
 
但是sort排序前会先进行一次判断,如果数组长度<286,就不会使用快速排序,而使用插入排序。
1 /**
2 * If the length of an array to be sorted is less than this
3 * constant, Quicksort is used in preference to merge sort.
4 */
5 private static final int QUICKSORT_THRESHOLD = 286;

 

优化后的快速排序

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 @a:是一个待排序数组
 2 @left:是数组左边第一个元素的下标
 3 @right:是数组右边的最后一个元素下标
 4 
 5 private static void sort(int[] a, int left, int right, boolean leftmost) {
 6         int length = right - left + 1;
 7 
 8 // Use insertion sort on tiny arrays
 9         if (length < INSERTION_SORT_THRESHOLD) {    // INSERTION_SORT_THRESHOLD = 47
10 
11         if (leftmost) {
12         /*
13          * Traditional (without sentinel) insertion sort,
14          * optimized for server VM, is used in case of
15          * the leftmost part.
16          */
17         for (int i = left, j = i; i < right; j = ++i) {
18         int ai = a[i + 1];
19         while (ai < a[j]) {
20         a[j + 1] = a[j];
21         if (j-- == left) {
22         break;
23         }
24         }
25         a[j + 1] = ai;
26         }
27         } else {
28         /*
29          * Skip the longest ascending sequence.
30          */
31         do {
32         if (left >= right) {
33         return;
34         }
35         } while (a[++left] >= a[left - 1]);
36 
37         /*
38          * Every element from adjoining part plays the role
39          * of sentinel, therefore this allows us to avoid the
40          * left range check on each iteration. Moreover, we use
41          * the more optimized algorithm, so called pair insertion
42          * sort, which is faster (in the context of Quicksort)
43          * than traditional implementation of insertion sort.
44          */
45         for (int k = left; ++left <= right; k = ++left) {
46         int a1 = a[k], a2 = a[left];
47 
48         if (a1 < a2) {
49         a2 = a1; a1 = a[left];
50         }
51         while (a1 < a[--k]) {
52         a[k + 2] = a[k];
53         }
54         a[++k + 1] = a1;
55 
56         while (a2 < a[--k]) {
57         a[k + 1] = a[k];
58         }
59         a[k + 1] = a2;
60         }
61         int last = a[right];
62 
63         while (last < a[--right]) {
64         a[right + 1] = a[right];
65         }
66         a[right + 1] = last;
67         }
68         return;
69         }

 

 
 
 
posted @ 2018-08-24 14:25  WuCola  阅读(607)  评论(2)    收藏  举报