Java. util. Arrays sorting program implementation code (1/2)

Source: Internet
Author: User

Java. UI. Arrays contains various methods used to operate Arrays (such as sorting and searching. In this article, we will study some sort algorithms written by masters.

(1) sorting of basic data type Arrays, such as Arrays. sort (int. A fine-tuned quick sorting is adopted. This algorithm is adapted from Jon L. bentley and M. engineering a Sort Function co-authored by Douglas McIlroy ", Software-Practice and Experience Vol. 23 (11) P. 1249-1265 (November 1993 ). This algorithm provides n * log (n) performance on many datasets, which causes other quick sorting to reduce the quadratic performance.
The source code for optimizing the Quick Sort Algorithm in JDK is as follows:

The Code is as follows: Copy code

/**
* Sorts the integer array of the specified range in ascending order.
* X [] array to be sorted
* Off: starts sorting from the off element of the array.
* Len array Length
*/
Private static void sort1 (int x [], int off, int len ){
// Optimization 1: in small-scale (size <7) arrays, direct insertion of sorting is more efficient than fast sorting.
If (len <7 ){
For (int I = off; I <len + off; I ++)
For (int j = I; j> off & x [J-1]> x [j]; j --)
Swap (x, j, J-1 );
Return;
}
// Optimization 2: carefully select the partitioning element, that is, pivot
// If it is a small-scale array (size <= 7), take the intermediate element as the pivot.
// If it is an array of medium size (7 = <size <= 40), take the number of the intermediate size in the number at the first, middle, and last positions of the array as the pivot.
// If it is a large-scale array (size> 40), take a pseudo-medium number (in the middle of the number s) from the nine specified numbers)
Int m = off + (len> 1 );
If (len> 7 ){
Int l = off;
Int n = off + len-1;
If (len> 40 ){
Int s = len/8;
L = med3 (x, l, l + s, l + 2 * s );
M = med3 (x, m-s, m, m + s );
N = med3 (x, N-2 * s, n-s, n );
}
M = med3 (x, l, m, n );
}
Int v = x [m];
// Optimization 3: each pivot v Division forms a form such as (<v) * v * (> v )*
// Stage 1: form an array of v * (<v) * (> v) * v *
Int a = off, B = a, c = off + len-1, d = c;
While (true ){
While (B <= c & x [B] <= v ){
If (x [B] = v)
Swap (x, a ++, B );
B ++;
}
While (c> = B & x [c]> = v ){
If (x [c] = v)
Swap (x, c, d --);
C --;
}
If (B> c)
Break;
Swap (x, B ++, c --);
}
// Phase 2, swap the pivot and element equal to the pivot to the center of the array
Int s, n = off + len;
S = Math. min (a-off, B-a); vecswap (x, off, B-s, s );
S = Math. min (d-c, n-d-1); vecswap (x, B, n-s, s );
// Phase 3: recursive sorting and pivot are not equal to element intervals
If (s = B-a)> 1)
Sort1 (x, off, s );
If (s = d-c)> 1)
Sort1 (x, n-s, s );
}


 

★Optimization 1: in small-scale (size <7) arrays, direct insertion sorting is more efficient than fast sorting.

No sorting is the optimal comparison-based internal sorting summary in any case. The O (N ^ 2) level sorting seems to be much worse than all advanced sorting. But this is not the case. The sort () algorithm in Arrays provides a good example. When the size of the array to be sorted is very small (the size threshold in JDK is INSERTIONSORT_THRESHOLD = 7), direct insertion sorting is better than fast sorting and merging sorting is better.

This is simple. The array size is small, and the comparison times of simple algorithms are not much higher than those of advanced algorithms. On the contrary, advanced algorithms, such as fast sorting and Merge Sorting, use recursive operations, resulting in a higher operating cost.

1 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.