Quick sorting of exchange sorting (implemented in Java)

Source: Internet
Author: User

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]

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.