Java sorting implementation experience

Source: Internet
Author: User

1. Overview

Sorting and searching are two very basic problems in programming. Now there are many classic algorithms used to solve these two problems, this article mainly discusses the implementation of sorting algorithms in java, hoping to serve as a reference. Before that, I would like to ask you a few questions: can you write a correct quick ranking? What is the real speed? Are you fast enough? Can it be further optimized? With these questions, let's take a look at how jre7 is implemented.

The implementation class of sorting in Jre7 is dualpolictquicksort. java has some changes compared with jre6, mainly in two places. One is the implementation of insertion sort, and the other is the implementation of QuickSort, in which the handler changes from one to two. Taking an array of the int type as an example, this class has a core method for sorting implementation. The prototype of this method is

VoidSort (Int[],IntLeft,IntRight,BooleanLeftmost)

Parameter a is an array to be sorted. left represents the index of the leftmost element in the array range to be sorted, and right represents the index of the rightmost element in the range, leftmost indicates whether the range is the leftmost range in the array. For example:

Array: [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 the () interval, left = 0, right = 2, leftmost = true

For the {} range, left = 3, right = 4, leftmost = false, the corresponding parameter of the <> range is also available.

When the Interval Length is less than 47, This method uses insert sorting; otherwise, uses quick sorting.

2. Insert sorting implementation

When leftmost is true, it uses the traditional insertion sorting (traditional insertion sort), and the Code is also relatively simple. The process is similar to card-catching cards:

For(IntI = left, j = I; I <right; j = ++ I ){

IntAi = a [I + 1];

While(Ai <a [j]) {

A [j + 1] = a [j];

If(J -- = left ){

Break;

}

}

A [j + 1] = ai;

}

Traditional insert sorting code

When leftmost is false, it uses a new type of insert sorting (pair insertion sort). The improvement is that two elements need to be inserted each time the sorted array is traversed, in the traditional insertion sorting process, you only need to find a proper position for an element to insert. For insert sorting, the key is to find a proper insert position for the element to be inserted. To find this position, we need to traverse the sub-arrays that have already been sorted. Therefore, for insert sorting, the number of elements traversed in the entire sorting process determines its performance. Obviously, inserting two elements at a traversal operation can reduce the number of elements traversed during the sorting process. The implementation code is as follows:

For(IntK = left; ++ left <= right; k = ++ left ){

IntA1 = 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 do the left-side intervals adopt the traditional insertion sorting and the rest adopt the pair insertion sorting? What is the problem when I use the insert sort code in pairs to replace the traditional insert sort code? We look forward to your answers...

3. Fast sorting implementation

In Jre7, quick sorting is also improved. The traditional quick sorting method is to select a sequence (jre6 method to select the elements in the leftmost, middle, and rightmost positions of the array, use the element with the value in the middle as the limit), and traverse from both ends to the center, exchange the values larger than the vertex value in the traversal process on the left and those smaller than or equal to the vertex value in the traversal process on the right. After the traversal encounters, insert the vertex value; in this way, the value on the left side of the sequence is less than or equal to the sequence, and the value on the right side of the sequence is greater than the sequence. Then, the left and right sides are sorted recursively.

Through the above analysis, we can see that each step of insertion sorting is to make a subinterval of the array absolutely ordered, and the essence of each loop is to make this subinterval continuously expand, so we can see that the optimization direction is to make every loop traversal as fast as possible to increase the speed of the subinterval, so the above inserts an element into each traversal to insert two elements at a time. Of course, some may ask, why don't we make this number bigger? For example, insert 5 or 10 in each traversal... Obviously, this is not the case. An extreme case is that n records are inserted every traversal (n is the length of the array )... As for why, let's answer it by yourself.

For fast sorting, what each recursion does is to make the subintervals to be sorted more orderly, rather than absolute order; so for fast sorting, its performance depends on the degree to which each recursive operation orders subintervals. Another factor is the number of recursion. The key to fast sorting to make subintervals more orderly is attention. Therefore, we should optimize the direction of attention, so we can increase the number of orders. We can also find that, increasing the number of sequences does not have much impact on the number of recursion attempts. Sometimes, the number of recursion times can be reduced. The problem similar to insert sort is that how many values are added to the sequence? Obviously, the value of the SWAp cannot be too large. Remember, any optimization is costly, and the cost of adding the SWAp is hidden in the position of each element of the swap. Guan Zi seems to be selling a little big... Next let's take a look at how the quick sorting is implemented when the value of the sort is 2. The implementation process is not hard to understand:

1. first, select two segments. The Selection Method of segments is to divide the array into six segments with the length of myopia, and these six segments are actually separated by five elements, sort the Five Elements from small to large, and take 2nd and 4th as values t1 and t2 respectively;

2. after the cursor is selected, the cursor is traversed from the left and right sides to the middle. The condition for the left traversal to stop is that a value greater than or equal to the T1 value is met, and the position is marked as less; the Stop Condition of the traversal on the right is that a value smaller than or equal to limit T2 is encountered, and the position is marked as great.

3. Traverse backward from the less position. k indicates the traversal position. The following situations may occur:

A. if the value at the k position is smaller than the value at the T1, the values at the k position and the less position are exchanged, and the less value is added with 1. In this way, the value on the left of the less position is smaller than that at the T1 position, the value between the less position and the k position is greater than or equal to T1.

B. if the value at the k position is greater than limit T2, traverse from the great position to the left. The traversal stop condition is that a value smaller than or equal to limit T2 is encountered. If the value is smaller than limit T1, write this value to the less location, write the value of the less location to the k Location, write the value of the k location to the great location, and finally less ++, great --; if the value is greater than or equal to T1, the k position and great position are switched, and then the great-

4. After completing the above process, the subintervals with sorting are divided into three sections ( T2.

/*

* Partitioning:

*

* Left part center part right part

* + -------------------------------------------------------------- +

* | <Required T1 | required t1 <= & <= required T2 |? |> Required T2 |

* + -------------------------------------------------------------- +

* ^

* |

* Less k great

*

* Invariants:

*

* All in (left, less) <limit T1

* Required t1 <= all in [less, k) <= required T2

* All in (great, right)> interval T2

*

* Pointer k is the first index? -Part.

*/

The core content of sorting implementation in Jre7 is described above. For details, refer to the Code in the corresponding class. If you find any errors or errors, please correct them.

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.