Select sorting directly (implemented in Java)

Source: Internet
Author: User

Import java. util. arrays;

Class datawraper implements comparable <datawraper> {
Int data;
String flag;

Public datawraper (INT data, string flag ){
Super ();
This. Data = data;
This. Flag = flag;
}
Public String tostring (){
Return data + flag;
}

@ Override
Public int compareto (datawraper DW ){
// Todo auto-generated method stub
Return this. Data> DW. Data? 1 :( this. Data = DW. Data? 0:-1 );
}

}
Public class selectsort {
// Select sorting directly
Public static void selectsort (datawraper [] data ){
System. Out. println ("start sorting ");
Int arraylength = data. length;
For (INT I = 0; I <arrayLength-1; I ++ ){
Int minindex = I;
For (Int J = I + 1; j <arraylength; j ++ ){
If (data [I]. compareto (data [J])> 0 ){
Datawraper TMP = data [I];
Data [I] = data [J];
Data [J] = TMP;
}
}
System. Out. println (Java. util. arrays. tostring (data ));
}
}

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 ));
Selectsort (data );
System. Out. println ("sorted: \ n" + arrays. tostring (data ));
}

}

Result:

Before sorting:
[21, 30, 49, 30 *, 16, 9]
Start sorting
[9, 30, 49, 30 *, 21, 16]
[9, 16, 49, 30 *, 30, 21]
[9, 16, 21, 49, 30, 30 *]
[9, 16, 21, 30, 49, 30 *]
[9, 16, 21, 30, 30 *, 49]
After sorting:
[9, 16, 21, 30, 30 *, 49]

After improvement:

Import java. util. arrays;

Class datawraper implements comparable <datawraper> {
Int data;
String flag;

Public datawraper (INT data, string flag ){
Super ();
This. Data = data;
This. Flag = flag;
}
Public String tostring (){
Return data + flag;
}

@ Override
Public int compareto (datawraper DW ){
// Todo auto-generated method stub
Return this. Data> DW. Data? 1 :( this. Data = DW. Data? 0:-1 );
}

}
Public class selectsort {
// Select sorting directly
Public static void selectsort (datawraper [] data ){
System. Out. println ("start sorting ");
Int arraylength = data. length;
// Perform n-1 comparison in sequence. the I-th comparison selects the I-th value and places it in the I position.
For (INT I = 0; I <arrayLength-1; I ++ ){
// Minindex always stores the minimum index in the current comparison.
Int minindex = I;
// The I-th data only needs to be compared with the data after it
For (Int J = I + 1; j <arraylength; j ++ ){
// If the minindex data is greater than the J data
If (data [minindex]. compareto (data [J])> 0 ){
Minindex = J;
}
}
// Each trip can be exchanged at most once.
If (minindex! = I ){
Datawraper TMP = data [I];
Data [I] = data [minindex];
Data [minindex] = TMP;
}
System. Out. println (Java. util. arrays. tostring (data ));
}
}

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 ));
Selectsort (data );
System. Out. println ("sorted: \ n" + arrays. tostring (data ));
}

}

Result:

Before sorting:
[21, 30, 49, 30 *, 16, 9]
Start sorting
[9, 30, 49, 30 *, 16, 21]
[9, 16, 49, 30 *, 30, 21]
[9, 16, 21, 30 *, 30, 49] // not exchanged
[9, 16, 21, 30 *, 30, 49] // not exchanged
[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.