Quick Sort (Java)

Source: Internet
Author: User

Import Java.util.Arrays;
public class Cao45
{

/**
* Fast Sorting
*
Thought
* The data to be sorted by a trip is divided into two separate parts, one of which is smaller than the number of the other part,
* And then follow this method to the two parts of the rapid sorting, the entire sorting process can be recursive,
* In order to achieve the entire data into an ordered sequence
*
* Three pointers:
* The first pointer is the Pivotval pointer (the middle pointer), the second pointer to the left and the third pointer right, respectively, to the leftmost and leftmost sequence,
* Left and right are simultaneously approaching the pivotval, while at the same time comparing with the median value, the elements smaller than the median are moved to the lower end,
* will be larger than the middle value of the move to the high end, the median value will never change, and finally put in the middle, before the small big
*/
public static void Main (string[] args)
{
Int[] array = {49,38,65,97,76};
QuickSort (array,0,4);
System.out.println (arrays.tostring (array));

}
/**
* Recursive function
*/
public static void QuickSort (int[] a,int left,int right)
{
int pivot;
if (left < right)
{
Pivot as the middle value, the smaller element on the left, the larger element in the right
Pivot = partion (a,left,right);
Recursive calls to left and right arrays are quickly sorted until the order is completely correct
QuickSort (a,left,pivot-1);
QuickSort (A,pivot+1,right);
}
}
/**
Split function, one trip, quick line.
*/
public static int partion (int[] a,int left,int right)
{
int pivotval = A[left];
The median value is selected and never changes, eventually in the middle, before the small size
while (left < right)
{
while (left < right && A[right] >= pivotval)
Place the element that is smaller than the middle value on the left side, at which point the right bit is equal to NULL, waiting for a number that is larger than the middle value to complement
--right;
A[left] = A[right];
while (left < right && A[left] <= pivotval)
Place the element that is larger than the middle value on the right side, where the left bit is equal to NULL, waiting for a number that is larger than the middle value to complement
++left;
A[right] = A[left];
}
Left==right, two seats are remitted into an empty space, which completes a quick sequence, in which the vacancy is made up of the median value
A[left] = Pivotval;
return left;
}
}

Quick Sort (Java)

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.