Heap sorting of Reading Notes

Source: Internet
Author: User

Heap sorting.

The heap mentioned here is different from the heap in memory storage.

A big top heap is a complete binary tree with the largest keywords in the root of the tree. Its Subtrees also comply with the concept of a big top heap and can be implemented using Arrays for storage.

 


A major purpose of heap is to find the maximum key word in the table very quickly. Just take the root of the tree.

 

 

1. The first step of heap sorting is:Heap Creation

Adjust the unordered table to a large top heap:

File buildheap. c

# Include "heapsort. H "<br/>/* create a large top heap */<br/>/* use an array to store the heap. The left child of sqlist [I] Is sqlist [2 * I], <br/> the right child is [2 * I + 1], and the parent node is sqlist [I/2] <br/> return value: heap top element, sqlist is already heap */<br/> int buildheap (int * sqlist, int Len) <br/>{< br/> If (LEN <0 | (sqlist = NULL) <br/> return (0 ); <br/>/* adjust the heap from a non-leaf node */<br/> for (INT I = (LEN/2); I> 0; I --) <br/> {<br/>/* if the user is compared with the parent and child, <br/> check whether the tree of the exchanged child meets the Heap Structure */<br/> for (Int J = I; (2 * j) <= Len;) // ensure that J is not a leaf node. <Br/>{< br/> int maxchild = 0; <br/>/* maxchild points to the one with the largest values of the two children */<br/> If (2 * j + 1) <= Len) // If J has two children <br/> maxchild = (sqlist [2 * j]> sqlist [2 * j + 1])? (2 * j) :( 2 * j + 1); <br/> else <br/> maxchild = 2 * J; // only the left child <br/> If (sqlist [maxchild]> sqlist [J]) <br/>{< br/> Exchange (sqlist, J, maxchild ); <br/> J = maxchild; // you need to check whether the Heap Structure of the Child tree is damaged. <br/>}< br/> else <br/> break; <br/>}< br/>}// for, all non-leaf nodes have been adjusted, and the heap has been established <br/> return (sqlist [1]); // return the heap top and the maximum value in the heap <br/>}

 


2. The second is to take the root of the Big Top heap, that is, delete the root of the tree, and then adjust the heap to make it conform to the structure of the Big Top heap.Delete heap top Element

File delheap. c



# Include "heapsort. H "<br/>/* Delete the heap top element sqlist [1] */<br/>/* return: 0 is returned for the error, and the deleted heap top element is returned correctly, <br/> and [1, len-1] re-adjusted piles, the deleted heap top elements are stored in sqlist [Len] */<br/> int delheap (int * sqlist, int Len) <br/>{< br/> If (LEN <0 | (sqlist = NULL) <br/> return (0 ); <br/> sqlist [0] = sqlist [1]; <br/> Exchange (sqlist, 1, Len ); // Delete the heap top element </P> <p> int end = len-1; <br/> for (INT I = 1; 2 * I <= end ;) <br/> {<br/> int maxchild = 0; <br/>/* maxchild points to the one with a relatively large value in the two children * /<Br/> If (2 * I + 1 <= end) // have two children <br/> maxchild = (sqlist [2 * I]> sqlist [2 * I + 1])? (2 * I): (2 * I + 1); <br/> else <br/> maxchild = 2 * I; // only the left child <br/> If (sqlist [I] <sqlist [maxchild]) <br/> {<br/> Exchange (sqlist, I, maxchild ); <br/> I = maxchild; // check whether the Heap Structure of the Child tree is damaged. <br/>}< br/> else <br/> break; <br/>}< br/> return (sqlist [Len]); <br/>}

 

 

 

3. Sort the heap.Heap sorting

File heapsort. c

# Include "heapsort. H "<br/>/* sort the unordered table's top heap, with a range of [1, Len] and a length of Len + 1 */<br/>/* sqlist: unordered table <br/> Len: unordered table length, range [1, Len] <br/> non-descending order, stability not considered */<br/> int heapsort (int * sqlist, int Len) <br/> {<br/> buildheap (sqlist, Len ); // heap creation <br/> for (INT I = Len; I> 0; I --) <br/>{< br/> delheap (sqlist, I ); <br/>}< br/> return (1)

 

 

File heapsort. h

# Include <time. h> // contains the time function <br/> # include <stdio. h> // printf () function <br/> # include <stdlib. h> // rand () function <br/> # include <stddef. h> // contains null <br/> # define max_length 100 <br/> # define max (x, y) (x)> (y ))? (X) :( y) <br/>/* function declaration */<br/> extern int heapsort (int * sqlist, int Len ); // heap sorting <br/> extern int delheap (int * sqlist, int Len); // Delete the heap top element <br/> extern int buildheap (int * sqlist, int Len); // heap creation <br/> extern void Exchange (int * sqlist, int left, int right); // exchange two elements in the array

 

 

File main. c

# Include "heapsort. H "<br/> int main () <br/> {<br/> int I, j = 0; <br/> int arraynumber [max_length + 1]; <br/>/* generate an unordered table */<br/> printf ("array number fallow as:/N"); <br/> srand (unsigned) time (null); // If put in for, only the same number will be generated <br/> for (I = 1, j = 1; I <= max_length; I ++) <br/>{< br/>/* randomly generate a number between 100 and */<br/> // arraynumber [I] = + rand () % (199-130 + 1); <br/>/* randomly generated Number */<br/> arraynumber [I] = 10 + rand () % (999-10 + 1); <br/> printf ("% 5d", arraynumber [I]); <br/> If (0 = (I % 10) <br/> {<br/> J = J + 1; <br/> printf ("/N"); <br/>}// If <br/>}// for <br/> heapsort (arraynumber, max_length ); <br/> printf ("The Heap Sort follow:/N"); <br/> for (I = 1; I <= max_length; I ++) <br/>{< br/> printf ("% 5d", arraynumber [I]); <br/> If (0 = (I % 10 )) <br/> printf ("/N"); <br/>}// for <br/> return 0; <br/>}

 

 

Result example:

[Lanux @ localhost -- debug -- 16: 30] $./heapsort
Array number fallow:
597 28 530 832 822 433 127 35 466
402 255 557 753 555 624 875 367 435 412
33 692 365 827 333 837 618 626 760 316
218 949 334 738 383 748 171 500 434 196
559 826 43 116 181 588 730 56 547
459 570 51 416 990 967 845 608 593
516 403 157 443 734 530 783 895 623 217
684 182 635 319 880 806 500 212 853 47
959 904 210 602 912 200 569 757 400 163
955 907 556 704 942 882 826 725 778 51
The Heap Sort follow:
28 33 35 43 47 51 51 56 93 116
127 157 163 171 181 182 196 200 210 212
217 218 255 316 319 333 334 365 367 383
400 402 403 412 416 433 434 435 443 459
466 500 500 516 530 530 547 555 556 557
559 569 570 588 593 597 602 605 608 618
623 624 626 635 684 692 704 725 730 734
738 748 753 757 757 760 778 783 806 822
826 826 827 832 837 845 853 875 880 882
895 904 907 912 942 949 955 959 967 990
[Lanux @ localhost -- debug -- 16: 31] $

17:19:13

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.