Heap sorting (source code + detailed comments)

Source: Internet
Author: User

  /* <Br/> ary is the array for storing records, start is the root record subscript to be adjusted to the top heap, and end is the subscript of its last leaf record <br/>. <Br/> Note: apart from the root records, the input records between start and end are all left and right sub-binary trees of the root records. <br/>, this function is valid only when it is fully compliant with the nature of the big top stack. <Br/> the following function is to adjust the full Binary Tree With start as the root record and end as the last leaf record as the big top heap. <Br/> */<br/> void heapify (INT ary [], unsigned int start, unsigned int end) <br/>{< br/> unsigned int left = 0; <br/> unsigned int right = 0; <br/> unsigned int max = 0; <br/> left = start * 2; <br/> right = start * 2 + 1; <br/>/* If the left sub-record */<br/> while (left <= END) <br/>{< br/>/* indicates that the Left and Right sub-records are in, first, the keyword value of the Left subrecord is the largest by default. Save Its subscript */<br/> max = left; <br/>/* if both the left and right subrecords exist, rewrite Max to save the subscript with the maximum keyword value record */<br/> If (right <= END) <br/> {<Br/> If (ary [left] <ary [right]) <br/> {<br/> max = right; <br/>}< br/> else <br/>{< br/> max = left; <br/>}< br/>/* currently, the left and right subrecords are saved in MAX (assuming they exist) subscript with the maximum keyword value */<br/> If (ary [start] <ary [Max]) <br/> {<br/>/* the keyword value of the root record is smaller than the value of the left and right subrecords */<br/> ary [0] = ary [Max]; <br/> ary [Max] = ary [start]; <br/> ary [start] = ary [0]; <br/>/* change from the exchanged sub-records */<br/> Start = max; <br/>}< br/> else <br/> {<br/>/* the binary tree meets the nature of the Big Top heap. The adjustment is complete. */<Br/> break; <br/>}< br/> left = start * 2; <br/> right = start * 2 + 1; <br/>}< br/>/* adjusted to the last leaf node of the most binary tree. The binary tree also conforms to the nature of the Big Top heap, adjusted to the end */<br/>}</P> <p>/* for heap sorting. After the completion, the subscripts of records in the ary are from 1 to size, their keyword values are not decreasing */<br/> void heapsort (INT ary [], unsigned int size) <br/> {<br/>/* heap creation */<br/> buildheap (ary, size); <br/> for (unsigned int I = size; i> 0; I --) <br/> {<br/>/* swap the root record with the last leaf record */<br/> ary [0] = ary [1]; <br/> ary [1] = Ary [I]; <br/> ary [I] = ary [0]; <br/>/* <br/> adjust the binary tree from 1 to I-1 as a big top heap. <Br/> Note: <br/> the exchange only destroys the Big Top heap of Binary Trees rooted in ary [1, its left and right <br/> sub-binary tree is still a backup Big Top heap. <Br/> */<br/> heapify (ary, 1, I-1 ); <br/>}</P> <p>/* <br/> adjust the entire binary tree (in a completely disordered state) to make it a big top heap. <Br/> Policy: <br/> first, the binary tree structure is a complete binary tree. We can adjust it from the last non-leaf record, <br/> continue to the root record of the entire binary tree. <Br/> for example, if a binary tree has a size node record, the subscript of the last Non-leaf node is <br/> size/2 (rounded up ), from size/2, size/2-1 ,..., <br/> the entire binary tree is regarded as the big top heap until the value of 1 is adjusted. <Br/> because the binary tree with the last leaf record as the root must meet the conditions for calling the heapify function, <br/> the non-leaf node on the same layer must also meet the call conditions, after the heapify function is processed, <br/> the binary tree corresponding to the non-leaf records on the previous layer also meets the call conditions, <br/> in this way, the binary tree recorded as the root of the entire binary tree (I .e., Ary [1]) also conforms to the nature of the big top stack, <br/> the entire stack is built. <Br/> the root record of the entire binary tree is the record with the largest keyword value in the record set. <Br/> */<br/> void buildheap (INT ary [], unsigned int size) <br/> {<br/>/* The input subscript is I (size/2> = I> = 1, <br/> adjust the binary tree of the last leaf record whose subscript is size */<br/> for (unsigned int I = size/2; I> 0; I --) <br/>{< br/> heapify (ary, I, size ); <br/>}</P> <p>/* <br/> <SPAN class = 'wp _ keywordlink '> algorithm </span> analysis: <br/> 1. the heapify time is mainly composed of the time overhead of establishing the initial heap and re-building the heap. <br/> both are implemented by calling heapify. <Br/> the worst time complexity of heap sorting is O (n * lgn ). The average performance of heap sorting is closer to the worst performance. <Br/> the time complexity of buildheap is O (n) in the worst case, but the for loop is O (n * lgn) in the worst case. <br/>. </P> <p> 2. Because the initial heap requires a large number of comparisons, the heap sorting is not suitable for files with a small number of records. </P> <p> 3. The heap sorting is local sorting, and the auxiliary space is O (1 ). </P> <p> 4. It is an unstable sorting method. <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.