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)