Heap sorting (implemented in Java)

Source: Internet
Author: User

Import java. util. arrays;

Public class heapsort {

Public static void heapsort (datawraper [] data ){
System. Out. println ("start sorting ");
Int arraylength = data. length;
// Create heap cyclically
For (INT I = 0; I <arrayLength-1; I ++ ){
// Create a heap
Buildmaxheap (data, arrayLength-1-i );
// Swap heap top and last element
Swap (data, 0, arrayLength-1-i );
System. Out. println (arrays. tostring (data ));
}
}

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;
}
// Create a large top heap for the data array from 0 to lastindex
Private Static void buildmaxheap (datawraper [] data, int lastindex ){
// Todo auto-generated method stub
// Start from the parent node of the node (last node) at lastindex
For (INT I = (lastIndex-1)/2; I> = 0; I --){
// K Save the node being determined
Int K = I;
// If the subnode of the current K-node exists
While (K * 2 + 1 <= lastindex ){
// Index of the Left subnode of K nodes
Int biggerindex = 2 * k + 1;
// If biggerindex is smaller than lastindex, that is, biggerindex + 1 indicates that the right subnode of the k node exists.
If (biggerindex <lastindex ){
// If the value of the right subnode is large
If (data [biggerindex]. compareto (data [biggerindex + 1]) <0 ){
// Biggerindex always records the index of a large subnode
Biggerindex ++;
}
}
// If the value of K node is smaller than the value of its larger subnode
If (data [K]. compareto (data [biggerindex]) <0 ){
// Exchange them
Swap (data, K, biggerindex );
// Assign biggerindex to K to start the next loop of the while loop, and re-ensure that the value of K nodes is greater than the value of its left and right subnodes.
K = biggerindex;
} Else {
Break;
}
}
}
}

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

}

Result

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