Heap Sort requires only one auxiliary space of the record size, and each record to be sorted occupies only one storage space.
(1) Basic Concepts
A) Heap: a sequence with n elements:
{K1, K2,..., kN}
For all I = 1, 2,..., (INT) (n/2), when the following relationship is satisfied:
Ki ≤ k2i, Ki ≤ k2i + 1
Or Ki ≥ k2i, Ki ≥ k2i + 1
Such a sequence is called a heap.
Two types of heap:
The smallest heap of the root node-the small heap.
The largest heap of the root node-a large heap.
The root node is called the heap top, that is, in a Complete Binary Tree, the values of all non-leaf nodes are less than (or greater than) The values of left and right children.
B) Heap sorting: it is a type of decision sorting, which is characterized by putting R [1 .. n] as a fully binary tree storage structure, using the internal relationship between the parent and child nodes of the fully Binary Tree, select the record with the maximum (minimum) keyword in the current unordered area.
(2) Heap sorting steps:
1, from the K-1 layer of the rightmost non-leaf node, so that the key value of the large (or small) records gradually move to the upper layer of the binary tree, the maximum (or small) the keyword record becomes the root node of the tree to make it a heap.
2. Gradually output the root node, so that R [1] = R [I] (I = N, n-1,..., 2), and adjust the remaining nodes in piles. Until all nodes are output. We call the adjustment process from heap to leaf as "screening ".
(3) Two problems to be solved:
1. How to Build a heap from an unordered sequence;
2. How to adjust the remaining elements into a heap after outputting a root node.
Building a disordered sequence into a heap is a process of repeated "screening. If we regard this sequence as a Complete Binary Tree, the last non-terminal node is the floor (n/2) element. Therefore, we only need to "filter" from the floor (n/2) element.
In heap sorting, a secondary space of the record size is required. Each record to be sorted occupies only one storage space. The heap sorting method is not recommended when there are few records. When N is large, the efficiency is very high. Heap sorting is unstable.
The heap Sorting Algorithm and filtering algorithm are shown in section 2. In order to make the sorting result non-descending and ordered, we first create a "big top Heap" in the sorting algorithm, that is, we first select a record with the maximum keyword and exchange it with the last record in the sequence, then, filter the first n-1 records in the sequence and adjust them to a "big top Heap". Then, the selected keyword is the largest record (that is, the first element) exchange with the current last record (globally n-1 records) until the sorting ends. From top to bottom, the filtering should be performed based on the child node with a large keyword.
The heap sorting algorithm is described as follows:
The C language code is as follows:
# Include "iostream" using namespace STD; # define maxsize 20 typedef struct {int key; // other data information} redtype; typedef struct {redtype R [maxsize + 1]; int length;} sqlist; typedef sqlist heaptype; // heap uses sequential table store to indicate void heapadjust (heaptype & H, int S, int m) // known h. R [s... m] records the keyword H. R [s]. the heap definition is satisfied except the key. This function is used to adjust H. R [s] keyword, so that H. R [s... m] into a large top heap (For the record keywords) {Int J; redtype RC; rc = H. R [s]; for (j = 2 * s; j <= m; j * = 2) // filter down the child nodes with large keys {If (j <M & (H. R [J]. key <H. R [J + 1]. key) // J indicates the subscript of the record with a large key + + J; If (RC. key> = H. R [J]. key) // RC should be inserted in position s break; H. R [s] = H. R [J]; // exchange the large nodes of the left and right children with the parent nodes to build a large top heap S = J;} H. R [s] = RC; // insert} void heapsort (heaptype & H) // perform heap sorting on the sequence table H {int I; for (I = H. length/2; I> 0; -- I) // a large top heap is built by an unordered sequence, and the sequence is regarded as a Complete Binary Tree, the last non-terminal node is the n/2 element heapadjust (H, I, H. length); for (I = H. length; I> 1; -- I) {H. R [0] = H. R [1]; // records the heap top and the unsorted subsequence H. R [1... i] the last record exchanges with each other. h. R [1] = H. R [I]; H. R [I] = H. R [0]; heapadjust (H, 1, I-1); // set h. R [1... i-1] Reset to Big Top heap} // heapsortvoid inputl (sqlist & L) {int I; printf ("Please input the length:"); scanf ("% d ", & L. length); printf ("Please input the data needed to sort: \ n"); for (I = 1; I <= L. length; I ++) // It is stored from the first subscript of the array, and the first subscript is used as a temporary variable for switching scanf ("% d", & L. R [I]. key);} void outputl (sqlist & L) {int I; printf ("the data after sorting is: \ n"); for (I = 1; I <= L. length; I ++) printf ("% d", L. R [I]. key); printf ("\ n");} int main (void) {sqlist h; inputl (h); heapsort (h); outputl (h ); system ("pause"); Return 0 ;}
Another method without using the struct above is as follows:
/** Heap sorting */# include "iostream" using namespace STD; # define n 10int array [N]; void man_input (int * array) {int I; for (I = 1; I <= N; I ++) {printf ("array [% d] =", I); scanf ("% d ", & array [I]) ;}} void myswap (int * a, int * B) // exchange {int temp; temp = * A; * A = * B; * B = temp;} void heap_adjust (int * heap, int root, int Len) // adjust the heap, make the unordered sequence of subscript from root to Len into a large top heap {int I = 2 * root; int T = heap [root]; while (I <= Len) {if (I <Len) {If (heap [I]