Java sort implementation of experience sharing _java

Source: Internet
Author: User
Tags array length

1. Overview
Sorting and searching are two kinds of very basic problems in program design, and there are many classical algorithms to solve these two kinds of problems, this paper mainly discusses the implementation of the ranking algorithm in Java, hoping to play a useful role. Before that, let me ask you a few questions: can you write a correct quick row? What's the real quick? Is your line fast enough? Can you further optimize it? With these questions, let's see how the Jre7 is achieved.

The implementation class for sorting in Jre7 is Dualpivotquicksort.java, with some changes compared to jre6, which occurs mainly in two places, one is insertion sort implementation, and the other is quicksort implementation pivot from one to two. We take an array of int as an example, and there is a core method for sorting implementations in this class, which is a prototype of

Copy Code code as follows:

void sort (int[] A, int left, int. right, Boolean leftmost)

Parameter A is an array that needs sorting, and left represents the index of the leftmost element in the array interval that needs to be sorted, right represents the index of the rightmost element in the interval, and leftmost represents whether the interval is the leftmost in the array. As an example:
Arrays: [2, 4, 8, 5, 6, 3, 0,-3, 9] can be divided into three intervals (2, 4, 8) {5, 6}<3, 0, -3, 9>
for () interval, left=0, right=2, leftmost=true
For {} intervals, left=3, right=4, Leftmost=false, the corresponding parameters of <> interval can be

When the interval length is less than 47 o'clock, the method will be sorted by inserting;

2. Insert Sort Implementation
When leftmost is true, it takes the traditional insertion sort (traditional insertion sort), and the code is simpler, and the process is similar to a card-grabbing tag when playing cards:

Copy Code code as follows:

for (int i = left, j = i; i < right; j = ++i) {
int ai = a[i + 1];
while (Ai < a[j]) {
A[j + 1] = A[j];
if (j--= left) {
Break
}
}
A[j + 1] = AI;
}

Traditional Insert Sort Code
When leftmost is false, it uses a new type of insertion sort (pair insertion sort), where the improvement is that each traversal of the previously sorted array requires two elements to be inserted, while the traditional insertion sort only needs to find the appropriate insertion point for an element during traversal. For an insert sort, the key is to find the appropriate insertion position for the element to be inserted, and in order to find the location, you need to traverse the previously sorted array, so for the insertion sort, the number of elements traversed in the whole sort process determines its performance. Obviously, inserting two elements at a time can reduce the number of elements that are traversed in the sorting process, and the implementation code is as follows:

Copy Code code as follows:

for (int k = left; ++left <= right; k = ++left) {
int a1 = A[k], a2 = A[left];

if (A1 < A2) {
a2 = A1; a1 = A[left];
}
while (A1 < A[--k]) {
A[k + 2] = a[k];
}
A[++k + 1] = A1;

while (A2 < a[--k]) {
A[k + 1] = a[k];
}
A[k + 1] = A2;
}

Now there is a question: Why is the left-most interval in the traditional insertion sort, and the others using a pair insertion sort? What happens when you add a sort code that replaces the traditional insert sort code with the above pair? Look forward to your own answer ...
3. Quick Sort Implementation
The quick sort has also been improved in Jre7, the traditional fast sorting is to select a pivot (jre6 selection of pivot method is to select the most left, middle and far right of the number of elements in the middle of the value of the element as pivot), and then from both ends to the middle traverse, Exchange the value of greater than pivot encountered in the left traversal process and the value less than or equal to pivot encountered in the right traversal, and insert the value of pivot when the traversal encounters; This makes the value on the left side of the pivot less than or equal to the value on the right side of the Pivot,pivot greater than pivot , then recursively sort the left and right, respectively.

Through the above analysis, we can see that each step in the insertion order is to make an array of children in absolute order, and the nature of each cycle is to enlarge the child interval, so we can see that the direction of optimization is to make each cycle of traversal as much as possible to expand the child range faster, So the above inserts each traversal into an element to optimize the insertion of two elements at a time. Certainly some people would ask, why not make this number a little bigger? For example, each traversal inserts 5, 10 ... Obviously, this is not the case, an extreme situation is that each traversal inserts n (n array length) ... As for why, let's answer it yourself.

For a quick sort, what each recursion does is to make the subgroups that need to be sorted more orderly rather than absolutely orderly; so for fast sorting, the performance is determined by the degree to which each recursive operation makes the ordered child intervals orderly, and the other determinant is, of course, recursion times. The key to fast sorting is to make the sub range relatively orderly, pivot, so the direction of our optimization should also be in the pivot, then increase the number of pivot it, and we can find that the number of pivot increase, the number of recursion will not have much impact, and sometimes even reduce the number of recursion. The problem with the insert sort is how many pivot to add? Obviously, the value of pivot can not be too large; remember, any optimizations have a price, and the cost of increasing pivot is hidden in the process of exchanging elements. It seems to sell a bit big ... Let's take a look at the pivot value of 2 o'clock, and how fast sorting is achieved. The implementation process is not difficult to understand:
1. First select two Pivot,pivot selection method is to divide the array into myopic six paragraphs, and this six is actually divided by 5 elements, the 5 elements from small to large sort, remove the 2nd and 4th, respectively, as PIVOT1 and Pivot2;
2. After the selection of pivot, respectively, from the left and right to the middle of the traversal, the left traversal of the condition is to encounter a value greater than or equal to PIVOT1, and mark that position as less; the stop condition for the right traversal is to encounter a value less than or equal to Pivot2 and mark that position as
3. Then from the less position backward traversal, the location of the traversal by K, will encounter the following situations:
A. K position value is smaller than pivot1, then the value of K position and less position is exchanged, and is less value plus 1, so that the value to the left of the less position is less than pivot1, and the value between less position and K position is greater than or equal to PIVOT1
B. K position value is greater than Pivot2, then from the great position to the left traversal, traversal stop condition is encountered a value less than equal to Pivot2, if this value is less than pivot1, write this value to the less position, the less position of the value wrote K position, Write the value of K position great position, the last less++,great--, add this value is greater than or equal to pivot1, Exchange K position and great position, after great-
4. After the completion of the above process, the order of the subgroups are divided into three paragraphs (<pivot1, Pivot1<=&&<=pivot2,>pivot2), and finally to the three paragraphs by recursion on the line.

Copy Code code as follows:

/*
* Partitioning:
*
* Left Part center part right part
* +--------------------------------------------------------------+
* |  < PIVOT1 |    Pivot1 <= && <= Pivot2 |    ?  | > Pivot2 |
* +--------------------------------------------------------------+
*               ^                          ^       ^
*               |       | |
* Less k Great
*
* Invariants:
*
* All in (left, less) < PIVOT1
* Pivot1 <= all in [less, k) <= Pivot2
* All in (great, right) > Pivot2
*
* Pointer k is the ' the '-part.
*/

The core of the sort implementation in Jre7 is as described above, and the specific details can be found in the code in the corresponding class, such as errors or irregularities.

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.