Sorting by sorting algorithms

Source: Internet
Author: User

Heapsort is a sort algorithm designed by using the data structure of heap. A heap is a structure that is similar to a completely binary tree and meets the heap nature. That is, the key value or index of a subnode is always smaller than or greater than its parent node.

The heap sorting process is as follows:

1. Create a heap

2. Swap the beginning (maximum) and end of the heap

3. Reduce the heap size by 1 to adjust the top data of the new array to the corresponding position.

4. Repeat Step 2 until the heap size is 1

 

To build a heap, you must understand the binary tree model.

Steps for building a large heap,

Step 1: First, we find that the heap has two parent nodes (2, 3 );

Step 2: compare the two children (4, 5) of the parent node 2, and find that the number is 5.

Step 3: exchange the larger right child (5) with the parent node (2). Now the heap of the left child in step 3 is complete,

Step 4: Compare the left and right children (5, 1) under the second parent node (3), and find that the left child is 5.

Step 5: exchange the parent node (3) with the left child (5). Note that the heap may be damaged after the switch,

Follow the steps 1, 2, and 3 above to reconstruct the heap.

The final heap constructed is as follows:

Second, output a large heap.

So far, we have constructed the big root heap. How can we output it? The purpose of making a big root stack is to find the maximum value,

Then we swap the heap top (5) with the heap end (2), and then remove (5) from the root heap. Because the heap top is now (2 ),

Therefore, if the root heap is damaged, it must be re-constructed. After the construction, the maximum value will appear, and the value will be re-exchanged and eliminated. Finally, the values will be

Implementation Code:

Public class heapsort {/***** build heap * @ Param array collection to be sorted * @ Param parent node Index * @ Param length remove the maximum value when outputting the root heap */Public static void heapadjust (INT [] array, int parent, int length) {// temp Save the current parent node int temp = array [Parent]; // get the left child int child = 2 * parent + 1; while (Child <length) {// if the parent has the right child, determine whether the left child is smaller than the right child if (Child + 1 <length & array [child] <array [Child + 1]) Child ++; // If the Father's Day is greater than the child node, you do not need to exchange if (temp> = array [child]) break; // assign the value of a large subnode to the Father's Day node array [Parent] = array [child]; // then use the subnode as the Father's Day node, prevent re-constructing parent = child when the root heap is damaged; // find the left child node with a small Father's Day node, child = 2 * parent + 1 ;} // Finally, assign the Temp value to a large subnode to form a two-value exchange array [Parent] = temp ;} // heap sorting public static void heapsort (INT [] array) {// list. length/2-1: The number of parent nodes in the heap for (INT I = array. length/2-1; I> = 0; I --) {heapadjust (array, I, array. length);} // The final output heap element for (INT I = array. length-1; I> 0; I --) {// tune int temp = array [0] against the I-th element of the current heap. array [0] = array [I]; array [I] = temp; // The root heap may be damaged because of the exchange of two values. Therefore, heapadjust (array, 0, i) ;}} public static void main (string [] ARGs) {system. out. println (5/2); int [] array = {2, 5, 1, 8, 9, 3}; system. out. println ("Before sorting:" + arrays. tostring (array); heapsort (array); system. out. println ("sorted:" + arrays. tostring (array ));}}

 

Running result:

Before sorting: [2, 5, 1, 8, 9, 3]

After sorting: [1, 2, 3, 5, 8, 9]

Related Article

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.