Heap sorting + code implementation and heap sorting code
Heap sorting
Heap is a binary tree. The size of a single node is smaller than that of its left and right children.
Sorting Ideology
The elements to be arranged are treated as full Binary Trees and physically stored in one-dimensional arrays.
Two problems need to be solved to achieve heap sorting:
1. How do I initialize a messy full binary tree into a heap?
A: Starting from the last non-leaf node, use the node as the root node and make top-down adjustments to make it a heap. Then perform this operation on the second and third nodes until the first node is positive.
2. After the top element of the heap is output, how can I adjust the remaining element to a heap?
A: place the last node on the original root node and use it as the root node for the above adjustments.
Complexity Analysis.
Calculate the number of layers of a binary tree, from top to bottom, 1, 2, 3, 4 ......
Time-consuming operations include constructing the initial heap and adjusting the heap.
Top-down adjustments to the heap depth of h, up to 2 * (h-1) compared ).
During Heap initialization, the height of the Complete Binary Tree is h, and the total number of comparisons is
In summary, the worst case of heap sorting is O (1) + (2) = O (n * logn ).
Code
Heap sorting source code
The algorithm is as follows:
// Hsort. c
// Algorithms are from introduction to Algorithms
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Typedef unsigned long int ULong;
ULong heapSize = 0;
ULong Parent (ULong index ){
// Because the starting subscript of the array in C language is 0
// This is a little different from the code in introduction to algorithms.
// Index> 1 = I/2;
Return (index % 2 )? (Index> 1): (index> 1)-1 );
}
ULong Left (ULong index ){
// Index <1 = index * 2;
Return (index <1) + 1 );
}
ULong Right (ULong index ){
Return (index <1) + 2 );
}
Void swap (char * a, char * B ){
Char tmp = *;
* A = * B;
* B = tmp;
}
// Recursive comparison between array [index] and left and right subtree
// Replace array [index] with the maximum value
Void maxHeapify (char * array, ULong index ){
ULong largest = 0;
ULong left = Left (index );
ULong right = Right (index );
If (left <= heapSize) & (array [left]> array [index])
Largest = left;
Else
Largest = index;
If (right <= heapSize) & (array [right]> array [largest])
Largest = right;
If (largest! = Index ){
Swap (& array [index], & array [largest]);
MaxHeapify (array, largest );
}
}
// Initialize the heap and place each element in the array to an appropriate position.
// After completion, the element at the top of the heap is the maximum value of the array.
Void buildMaxHeap (char * array, ULong length ){
Int I;
HeapSize = length;
For (I = (length> 1); I> = 0; I --)
MaxHeapify (array, I );
}
Int heapSort (char * array, ULong length ){
Int I;
BuildMa ...... remaining full text>
[Data Structure in] instance for heap sorting (no code)
We assume that we plan to sort data from small to large, so we need to build a large top heap.
Initially, the heap is the whole sequence)
So the whole heap looks like:
15
14 5
7 12 18
At this time, the heap is created from the bottom triple.
Step 1: Step 2:
15 18
14 18 14 15
7 12 5 7 12 5
Then start sorting. At this time, 18 and 5 are exchanged. Then 18 is the largest number at the last position.
You do not need to continue heap sorting, so
5
14 15
7 12
The heap creation process is as follows:
15
14 5
7 12
And so on.
The difference between the first heap building process and the subsequent heap sorting process is that the first heap sequence is messy, so
From the last branch to the next heap, and the subsequent heap sorting process, because the heap already has
Except for the top element, the basic rule is basically ordered, so the heap process is performed from top to bottom.