The idea of the so-called quick sort is to first take out the first number of the array as a key, set a i,j as the identity before and after each, and then take the key to the array from behind, and j--, until we find the first number less than this key, and then exchange the two values, after the exchange is complete , we take this key to go backwards from I, and i++, loop until the end of the i=j, when this is over, we will find that the value greater than this key will go to the back of the key, not if you write the wrong, less than this key will run to the front of this value Then we can complete the ordering of the entire array by invoking the array recursively.
Represented by a graphical representation of the following:
This will be divided into two segments of the key, we will take these two paragraphs further to solve the problem.
The code is as follows:
Package test;
public class Testquicksort {
public static void Main (string[] args)
{
TODO automatically generate method stubs
Quicksort qs = new Quicksort ();
int data[] = {44,22,2,32,54,22,88,77,99,11};
Qs.data = data;
Qs.sort (0, qs.data.length-1);
Qs.display ();
}
}
Class Quicksort
{
public int data[];
private int partition (int sortarray[],int low,int hight)
{
int key = Sortarray[low];
while (Low{
while (Lowhight--;
Sortarray[low] = Sortarray[hight];
while (Lowlow++;
Sortarray[hight] = Sortarray[low];
}
Sortarray[low] = key;
return low;
}
public void sort (int low,int hight)
{
if (low{
int result = partition (Data,low,hight);
Sort (low,result-1);
Sort (result+1,hight);
}
}
public void display ()
{
for (int i=0;i<data.length;i++)
{
System.out.print (Data[i]);
System.out.print ("");
}
}
}
Understanding of quick Sort in Java and examples