Complete heap sorting code (continued)

Source: Internet
Author: User

Continued above,

The above initialization build heap and the replaced build heap are written separately.

The initialization starts from I = n/2 .. n to I = 1 .. n.

 

And after replacement is built from I = 1... n-1, I = 1... N-2...

The building start and end positions are different.

 

Therefore, you can synthesize a function. You only need to write a loop from I = n/2 To I = 1 when building the heap to complete the construction of each word.

 

The Code is as follows: Click it!

 

# Include <stdlib. h> <br/> # include <stdio. h> </P> <p> # define max_size 20 <br/> typedef int elemtype; </P> <p> // define the sorting table <br/> typedef struct heaplist <br/> {<br/> elemtype * keys; <br/> int length; <br/>} heaplist; </P> <p> // initialize the sorting table. <br/> void initheaplist (heaplist & L) <br/>{< br/> L. keys = (elemtype *) malloc (max_size * sizeof (elemtype); <br/> L. length = 6; <br/> int data [] = {38, 11, 9, 27, 96, 83}; <br/> for (INT I = 0; I <6; ++ I) <br/> L. keys [I + 1] = data [I]; <br/>}</P> <p> // print the sorting table <br/> void printheaplist (heaplist & L) <br/> {<br/> for (INT I = 1; I <= L. length; ++ I) <br/> printf ("% 5d", L. keys [I]); <br/> printf ("/N "); <br/>}</P> <p> // initialize heap <br/> void createbigheap (heaplist & L) <br/>{< br/> int middle = L. length/2; <br/> for (INT I = middle; I> 0; I --) <br/> {<br/> If (2 * I + 1)> L. length) & (2 * I <= L. length )) <br/> {<br/> // when no right-word tree exists, only the size of the root node and left-word tree are determined and swapped. <br/> If (L. keys [I] <L. keys [2 * I]) <br/>{< br/> L. keys [0] = L. keys [I]; <br/> L. keys [I] = L. keys [2 * I]; <br/> L. keys [2 * I] = L. keys [0]; <br/>}< br/> If (2 * I + 1) <= L. length) <br/> {<br/> // both left and right sub-numbers <br/> // compare the maximum left and right word trees <br/> int max = 2 * I; <br/> If (L. keys [2 * I] <L. keys [2 * I + 1]) Max ++; <br/> If (L. keys [I] <L. keys [Max]) <br/>{< br/> L. keys [0] = L. keys [I]; <br/> L. keys [I] = L. keys [Max]; <br/> L. keys [Max] = L. keys [0]; <br/>}</P> <p> // after the large heap is initialized, every time the previous N-1 is heap again <br/> void heapfy (heaplist & L, int end) <br/>{< br/> // go out to the root node at this time, the rest are consistent with the heap, and can be compared from the header knot and Its subnode pairs, always stop when there are no child nodes or when the heap is satisfied <br/> for (INT I = 1; 2 * I <= end; I * = 2) <br/> {<br/> If (2 * I + 1 <= END) <br/> {<br/> // compare the three items, then update I to replace it with <br/> int max = 2 * I; <br/> If (L. keys [2 * I] <L. keys [2 * I + 1]) Max ++; <br/> // if the tree on the right is the largest, change the node on the root of the word to another node. <br/> L. keys [0] = L. keys [I]; <br/> L. keys [I] = L. keys [Max]; <br/> L. keys [Max] = L. keys [0]; <br/> // remember to update the I position <br/> I = max; <br/> // jump out of if, enter the next loop of, at this time, continue to perform the above operations on the Self-tree of I <br/>}< br/> If (2 * I + 1> end & 2 * I <= end) <br/> {<br/> // only the left subtree is available. <br/> If (L. keys [I] <L. keys [2 * I]) <br/>{< br/> L. keys [0] = L. keys [I]; <br/> L. keys [I] = L. keys [2 * I]; <br/> L. keys [2 * I] = L. keys [0]; <br/>}</P> <p> // heap sorting <br/> void heapsort (heaplist & l) <br/>{< br/> // initialize a large heap <br/> createbigheap (L ); <br/> // Replace the first and last values. <br/> L. keys [0] = L. keys [1]; <br/> L. keys [1] = L. keys [L. length]; <br/> L. keys [L. length] = L. keys [0]; <br/> for (INT I = L. length-1; I> 0; -- I) <br/>{< br/> heapfy (L, I); <br/> L. keys [0] = L. keys [1]; <br/> L. keys [1] = L. keys [I]; <br/> L. keys [I] = L. keys [0]; <br/>}</P> <p> // after the large heap is initialized, each time a large heap of the previous N-1 is made again <br/> void heapsortevery (heaplist & L, int start, int end) <br/>{< br/> // go out to the root node at this time, and the rest are consistent with the heap. You can compare the headers with their child nodes, always stop when there are no child nodes or when the heap is satisfied <br/> for (INT I = start; 2 * I <= end; I * = 2) <br/> {<br/> If (2 * I + 1 <= END) <br/> {<br/> // compare the three items, then update I to replace it with <br/> int max = 2 * I; <br/> If (L. keys [2 * I] <L. keys [2 * I + 1]) Max ++; <br/> // if the tree on the right is the largest, change the node on the root of the word to another node. <br/> L. keys [0] = L. keys [I]; <br/> L. keys [I] = L. keys [Max]; <br/> L. keys [Max] = L. keys [0]; <br/> // remember to update the I position <br/> I = max; <br/> // jump out of if, enter the next loop of, at this time, continue to perform the above operations on the Self-tree of I <br/>}< br/> If (2 * I + 1> end & 2 * I <= end) <br/> {<br/> // only the left subtree is available. <br/> If (L. keys [I] <L. keys [2 * I]) <br/>{< br/> L. keys [0] = L. keys [I]; <br/> L. keys [I] = L. keys [2 * I]; <br/> L. keys [2 * I] = L. keys [0]; <br/>}</P> <p> void heapsort _ (heaplist & L) <br/> {<br/> // create a heap first <br/> for (INT I = L. length/2; I> 0; -- I) <br/> heapsortevery (L, I, L. length); <br/> L. keys [0] = L. keys [1]; <br/> L. keys [1] = L. keys [L. length]; <br/> L. keys [L. length] = L. keys [0]; <br/> for (Int J = L. length-1; j> 0; -- j) <br/>{< br/> heapsortevery (L, 1, J); <br/> L. keys [0] = L. keys [1]; <br/> L. keys [1] = L. keys [J]; <br/> L. keys [J] = L. keys [0]; <br/>}</P> <p> void main () <br/>{< br/> heaplist L; <br/> initheaplist (l); <br/> printheaplist (l); <br/> heapsort _ (L ); <br/> printheaplist (l); <br/>}

 

 

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.