Insert sorting-semi-insert sorting (implemented in Java)

Source: Internet
Author: User

Import java. util. arrays;

Public class binaryinsertsort {

Public static void binaryinsertsort (datawraper [] data ){
System. Out. println ("start sorting :");
Int arraylength = data. length;
For (INT I = 1; I <arraylength; I ++ ){
Datawraper TMP = data [I];
Int low = 0;
Int high = I-1;
While (low <= high ){
// Finds the middle indexes of low and high
Int mid = (low + high)/2;
// If the TMP value is greater than the values of the low and high elements
If (TMP. compareto (data [Mid])> 0 ){
// Search for more than half of the MID Index
Low = Mid + 1;
} Else {
// Search within half of the index smaller than the mid
High = mid-1;
}
}
// Move all the elements from low to I to one whole.
For (Int J = I; j> low; j --){
Data [J] = data [J-1];
}
Data [low] = TMP;
System. Out. println (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 ));
Binaryinsertsort (data );
System. Out. println ("sorted: \ n" + arrays. tostring (data ));
}

}

Result:

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