Today, I have carefully observed the java. util. arrays. sort method under 1.6jdk.
Summary:
Sort sorts basic data types (byte char short int float long double) and object types.
1. Sort Basic Data Types
The public methods in the arrays class are all sort,
But what actually works in it is sort2 and sort1.
Sort1 is the most practical task.
Sort2 does some boundary processing, similar
Private Static void sort2 (double A [], int fromindex, int toindex) {<br/> final long neg_zero_bits = double. doubletolongbits (-0.0d ); <br/>/* <br/> * the sort is done in three phases to avoid the expense of using <br/> * Nan and-0.0 aware comparisons during the main sort. <br/> */</P> <p>/* <br/> * preprocessing phase: Move any Nan's to end of array, count the <br/> * Number of-0.0's, and turn th Em into 0.0's. <br/> */<br/> int numnegzeros = 0; <br/> int I = fromindex, n = toindex; <br/> while (I <n) {<br/> if (a [I]! = A [I]) {<br/> double swap = A [I]; <br/> A [I] = A [-- N]; <br/> A [n] = swap; <br/>}else {<br/> if (a [I] = 0 & double. doubletolongbits (A [I]) = neg_zero_bits) {<br/> A [I] = 0.0d; <br/> numnegzeros ++; <br/>}< br/> I ++; <br/>}</P> <p> // main sort phase: quicksort everything but the Nan's <br/> sort1 (A, fromindex, N-fromindex); </P> <p> // postprocessing phase: change 0.0's to-0.0's as required <Br/> If (numnegzeros! = 0) {<br/> Int J = binarysearch0 (A, fromindex, N, 0.0d ); // posn of any zero <br/> do {<br/> j --; <br/>} while (j> = 0 & A [J] = 0.0d ); </P> <p> // J is now one less than the index of the first zero <br/> for (int K = 0; k <numnegzeros; k ++) <br/> A [++ J] =-0.0d; <br/>}< br/>}
Several core sort1 method declarations:
Private Static void sort1 (long x [], int off, int Len)
Private Static void sort1 (int x [], int off, int Len)
Private Static void sort1 (short X [], int off, int Len)
Private Static void sort1 (char X [], int off, int Len)
Private Static void sort1 (byte X [], int off, int Len)
Private Static void sort1 (Double X [], int off, int Len)
Private Static void sort1 (float X [], int off, int Len)
The implementation logic is basically the same. If the length of the array is less than 7, it is the simplest sort of insertion. Otherwise, the quick sort is used.
Because the time complexity of fast sorting in the worst case is O (n ^ 2), the selection of Logo elements is also exquisite,
Similar
If (LEN> 7) {<br/> int L = off; <br/> int n = off + len-1; <br/> If (LEN> 40) {// big arrays, pseudo median of 9 <br/> int S = Len/8; <br/> L = med3 (x, l, l + S, L + 2 * s); <br/> M = med3 (x, M-S, M, M + S); <br/> N = med3 (X, n-2 * S, N-S, N); <br/>}< br/> M = med3 (x, L, M, N); // mid-size, med of 3 <br/>}
Big arrays section in
2. Sort object data types by merging
Main Implementation Method
Private Static void mergesort (object [] SRC,
Object [] DEST,
Int low,
Int High,
Int off)
And
Private Static void mergesort (object [] SRC,
Object [] DEST,
Int low, int high, int off,
Comparator C)
These two,
If the length of the array is less than 7 during the merge process, insert the sort to order the array,
Otherwise, recursive merge is continued to complete sorting.