Import java. util. arrays;
Public class quicksort {
Public static void quicksort (datawraper [] data ){
System. Out. println ("start sorting ");
Subsort (data, 0, Data. Length-1 );
}
// Process the subsequences in the range from start to end index in the data array,
// Place all smaller than the demarcation value on the left, and all those greater than the demarcation value on the right
Private Static void subsort (datawraper [] data, int start, int end ){
// Todo auto-generated method stub
If (start <End ){
// Use the first element as the Boundary Value
Datawraper base = data [start];
// I search from the left and search for indexes of elements greater than the demarcation Value
Int I = start;
// J searches for the index of the element smaller than the boundary value from the right.
Int J = end + 1;
While (true ){
// Locate the element index that is greater than the boundary value, or I has reached the end.
While (I <End & Data [++ I]. compareto (base) <= 0 );
// Locate the element index smaller than the demarcation value, or J has arrived at start.
While (j> Start & Data [-- J]. compareto (base)> = 0 );
If (I <j ){
Swap (data, I, j );
} Else {
Break;
}
System. Out. println (arrays. tostring (data ));
}
// Exchange the boundary value with the element at the J Index
Swap (data, start, J );
System. Out. println (arrays. tostring (data ));
// Recursively traverse the left subsequence
Subsort (data, start, J-1 );
// Recursively traverse the right subsequence
Subsort (data, J + 1, end );
}
}
Private Static void swap (datawraper [] data, int I, Int J ){
// Todo auto-generated method stub
Datawraper TMP = data [I];
Data [I] = data [J];
Data [J] = TMP;
}
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Datawraper [] DATA = {
New datawraper (21 ,""),
New datawraper (30 ,""),
New datawraper (49 ,""),
New datawraper (30 ,"*"),
New datawraper (16 ,""),
New datawraper (9 ,""),
};
System. Out. println ("Before sorting: \ n" + arrays. tostring (data ));
Quicksort (data );
System. Out. println ("sorted: \ n" + arrays. tostring (data ));
}
}
Result:
Before sorting:
[21, 30, 49, 30 *, 16, 9]
Start sorting
[21, 9, 49, 30 *, 16, 30]
[21, 9, 16, 30 *, 49, 30]
[16, 9, 21, 30 *, 49, 30]
[9, 16, 21, 30 *, 49, 30]
[9, 16, 21, 30 *, 49, 30]
[9, 16, 21, 30 *, 30, 49]
After sorting:
[9, 16, 21, 30 *, 30, 49]