First, quick sort
1. Upgraded bubble sort + recursion
2. Compare when selecting a comparison pivot object so that its left is less than it, and the right side is greater than it.
3. Re-order data recursively on both sides of the pivot object
Second, the code example
public class QuickSort
{
public static int partition (int[] n, int. left, int. right)
{
int pivotkey = N[left];
Pivot is selected and will never change, eventually in the middle, before the small rear big
while (left < right)
{
while (left < right && N[right] >= pivotkey)
--right;
Move the element smaller than the pivot to the lower end, at which point the right bit is equal to NULL,
Wait for the lower than the PivotKey large number to fill up
N[left] = N[right];
while (left < right && N[left] <= pivotkey)
++left;
Move the element larger than the pivot to the high end, at which point the left bit is equal to NULL,
Waiting for a higher number to be smaller than the pivotkey.
N[right] = N[left];
}
When left = = right, complete a quick sort of a trip,
At this point the left bit is equivalent to empty, waiting for PivotKey to fill
N[left] = PivotKey;
SYSTEM.OUT.PRINTLN ("Pivot key:" + left);
System.out.println ("Current value:" + arrays.tostring (n));
return left;
}
/*
* First write the algorithm according to the array for the data prototype, and then write the extensibility algorithm.
* Array {49,38,65,97,76,13,27}
*/
public static void QuickSort (int[] n, int left, int. right)
{
int pivot;
if (left < right)
{
Pivot as pivot, compared to small elements on the left, compared to large elements in the right
Pivot = partition (n, left, right);
Recursive calls to left and right arrays are quickly sorted until the order is completely correct
QuickSort (n, left, pivot-1);
QuickSort (n, pivot + 1, right);
}
}
public static void Main (string[] args)
{
int[] Array =
{3,4,2,6,7,1,4,1,2,4};
QuickSort (array, 0, array.length-1);
for (int i = 0; i < array.length; i++)
{
System.out.println (Array[i]);
}
}
}
Java Quick Sort