Java sort of Fast row

Source: Internet
Author: User
This article to talk about Fast platoon, there is a feeling recently, as long as there are rules to follow the code, decomposition into two parts after the efficiency will improve a lot. The code thought is as follows
This code writes is the fast platoon, the quick row most main thought is seeks a boundary value, the big puts on one side, the small puts on the side, then handles the big and the small, recursively respectively,
Note here that what we need to move the cursor is to add a value equal to the boundary, otherwise, if there is an equal value, it goes into the dead loop.
In simple terms, when a number is the boundary value, then the same number of the other is not moving if not to the boundary

, but whether it is large or small, the recursive completion will always be next to the demarcation value.

Code:

Package www.jk.qsort;

Import Java.util.Arrays; /** * * @author JK This code is written in the fast row, the main thinking is to look for a boundary value, the big one side, the small side, and then recursively handle the large and small, * Note that we need to move the cursor is to add a value equal to the demarcation, otherwise
 If you have equal values, you will go into the dead loop. * Very simply, when a number is the boundary value, then another and his the same number if not to the boundary will not move the *, but whether the large or small recursive completion will be next to the demarcation value. * */public class Test {/** * @param args/public static void main (string[] args) {//TODO auto-generated m
		Ethod stub int[] A = new int[] {2, 1, 6, 7, 8, 5, 3, 5};
		QuickSort (A, 0, a.length-1);
	System.out.println (Arrays.tostring (a));
		private static void QuickSort (int[] A, int begin, int end) {//int tbegin = begin, tend = end;
		The first value as a fast-sorted boundary value int pivot = A[begin]; while (Tbegin < tend) {//If two cursors have no intersection, or the following is always greater than or equal to the boundary value, move forward while (Tbegin < tend && A[tend] >= pivo
			T) {--tend;
			} A[tbegin] = A[tend];
			If two cursors do not intersect, or if the preceding is less than or equal to the boundary value, it is moved forward while (Tbegin < tend && A[tbegin] <= pivot) {++tbegin; } A[tend] = A[tbegin];
		///Where the threshold value is assigned to the intersection of the cursor a[tbegin] = pivot;
		If (begin < tend) {//recursive sort cursor left quickSort (A, begin, tend-1);
		} if (Tbegin < end) {//QuickSort to the right of the recursive sort cursor (A, tbegin + 1, end);
 }

	}

}




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.