C ++ implements heap sorting and heap sorting
Heap sorting is a sort method that has the advantages of Merge Sorting and insert sorting. Its time complexity is O (nlgn), and it is also an in-situ Sorting Algorithm: at any time, only constant elements in the array are stored outside the input array. To introduce heap sorting, first introduce what is heap.
1. Heap Building: the heap data structure is an array object, which can be regarded as a Complete Binary Tree, such. The heap represented by the array on the right can be represented by a complete binary tree on the left. If the parent node corresponds to an array subscript of I, the array subscript of the left child is 2 * I, the right child is 2 * I + 1.
The Code is as follows:
Void buildMaxHeap (int a [], int heapSize ){
For (int I = heapSize/2; I> = 1; -- I)
Max_heapify (a, I, heapSize); // This line of code calls the max_heapify function to maintain the maximum heap nature. Next we will introduce it. HeapSize indicates the heap size.
}
2. Preserve the root heap nature: Next we will introduce the differences between a large and a small root heap. The value of a node in a large root stack is the same as that of its parent node. The organizational structure of the root heap is the opposite. In heap sorting, we use a large heap. To maintain the nature of the large root stack, we assume that the value of the array element whose subscript is I is compared with its left child and right child to find the subscript largest corresponding to the maximum value, if largest is exactly the same as I, it indicates that node I maintains the maximum heap nature. Otherwise, the array element values corresponding to largest and I will be exchanged, and the elements whose subscript is largest will continue the "keep the root heap property" operation. In this case, there may be too little vividness. Let's take a look at this process:
We can see that the process of retaining the root heap of nodes with the node value 4 in the figure. The Code is as follows:
Void max_heapify (int a [], int I, int heapSize ){
Int lindex = 2 * I, rindex = 2 * I + 1, largest = 0;
If (lindex <= heapSize & a [lindex-1]> a [I-1]) // identify who is the value of left child and node I, save the large worthy subscript as largest
Largest = lindex;
Else
Largest = I;
If (rindex <= heapSize & a [rindex-1]> a [largest-1]) // determine the value of the right child and node largest
Largest = rindex;
If (largest! = I) {// If largest is not equal to I
Swap (a [I-1], a [largest-1]); // exchange
Max_heapify (a, largest, heapSize); // continue to "maintain the root heap nature" for nodes whose subscript is largest and whose size is heapSize"
}
}
The following are swap functions:
Void swap (int & first, int & second ){
Int tem = 0;
Tem = first;
First = second;
Second = tem;
}
3. Heap Sorting Algorithm: I will use a chart to express it intuitively!
The process is to swap the root node of the large root heap with the last leaf node, then narrow down the array range (to reduce the heapSize), and eliminate the maximum value, finally, the new root node is "heap-preserving method", and so on. The Code is as follows:
Void heapSort (int a [], int heapSize ){
BuildMaxHeap (a, heapSize); // heap Creation
For (int I = heapSize; I> = 2; -- I ){
Swap (a [0], a [I-1]); // swap the root node with the final leaf node
-- HeapSize; // narrow the heap range.
Max_heapify (a, 1, heapSize); // preserve the heap nature
}
}
C language heap sorting
Void heapsort (int B [20])
Change the function parameter to the address or pointer of the array.
(C Language) using the heap Sorting Algorithm: To achieve the array subscript is 1 to n number from small to large output various help
# Include <stdio. h>
Void main ()
{
Int a [100];
Int I, j, m;
Int temp;
Printf ("how many digits do you want to enter: \ n % d ");
Scanf ("% d", & m );
Printf ("\ n ");
For (I = 0; I <m; I ++)
{
Scanf ("% d", & a [I]);
}
For (I = 0; I <m; I ++)
For (j = I + 1; j <m; j ++)
If (a [I]> a [j])
{
Temp = a [I];
A [I] = a [j];
A [j] = temp;
}
For (I = 0; I <m; I ++)
Printf ("% d \ n", a [I]);
}