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:Copy codeThe 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; // The heap uses sequential table Storage for representation.
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] becomes a big top heap (for the key words recorded in it)
{
Int j;
RedType rc;
Rc = h.r [s];
For (j = 2 * s; j <= m; j * = 2) // filter the child nodes with large keys downward.
{
If (j <m & (H. r [j]. key <H. r [j + 1]. key) // j indicates the subscript of a record with a large key.
++ J;
If (rc. key> = H. r [j]. key) // rc should be inserted on location s.
Break;
H. r [s] = H. r [j]; // exchange the large left and right nodes with the parent nodes to build a large top heap.
S = j;
}
H.r [s] = rc; // insert
}
Void HeapSort (HeapType & H) // sorts the heap of 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 exchange with each other
H.r [1] = h.r [I];
H.r [I] = h.r [0];
HeapAdjust (H, 1, I-1); // reset h.r [1... I-1] to a large top heap
}
} // HeapSort
Void 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 ++) // starts to store the first subscript of the array. The second subscript is used as a temporary variable for exchange.
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:Copy codeThe Code is as follows :/*
* Heap sorting
*/
# Include "iostream"
Using namespace std;
# Define N 10
Int 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 = * B;
* B = temp;
}
Void heap_adjust (int * heap, int root, int len) // adjust the heap to make the unordered sequence of subscript from root to len a large top heap.
{
Int I = 2 * root;
Int t = heap [root];
While (I <= len)
{
If (I <len)
{
If (heap [I] I ++;
}
If (t> = heap [I])
Break;
Heap [I/2] = heap [I];
I = 2 * I;
}
Heap [I/2] = t;
}
Void heapSort (int * heap, int len) // heap sorting
{
Int I;
For (I = len/2; I> 0; I --) // a large top heap is built from an unordered sequence, and the sequence is considered as a Complete Binary Tree, the last non-terminal node is the len/2 element.
{
Heap_adjust (heap, I, len );
}
For (I = len; I> = 1; I --)
{
MySwap (heap + I, heap + 1); // swap the heap top record with the last record
Heap_adjust (heap, 1, I-1); // set the subscript to 1 ~ Record of I-1 adjusted to Big Top heap
}
}
Void print_array (int * array, int n)
{
Int k;
For (k = 1; k <n + 1; k ++)
{
Printf ("% d \ t", array [k]);
}
}
Int main (void)
{
Man_input (array );
HeapSort (array, N );
Printf ("\ nAfter sorted by the heap_sort algorithm: \ n ");
Print_array (array, N); // print the heap sorting result
System ("pause ");
Return 0;
}