Sort | algorithm
5. Heap Sorting
5.1. Basic ideas:
Heap sort is a sort of tree-shape selection, in which the R[1..N] is regarded as a sequential storage structure of a complete binary tree, and the smallest element is selected by using the intrinsic relationship between the parent node and the child node in the complete binary tree.
5.2. Definition of heap:
The sequence of N elements is K1,k2,k3,..., Kn. Called a heap, if and only if the sequence satisfies the attribute: Ki≤k2i ki≤k2i+1 (1≤I≤[N/2)).
The heap is essentially a complete binary tree that satisfies the following properties: The key word for any node in the tree is greater than or equal to the child's node. For example, a sequence 10,15,56,25,30,70 is a heap, which corresponds to a complete binary tree as shown in the above figure. The root node in this heap (called the heap top) is the smallest keyword, and we call it a small Gan. Conversely, if the complete binary tree in any one of the nodes of the key words are greater than the equivalent of their children's keywords, it is called a large heap.
5.3. Sorting process:
Heap sorting is the use of small Gan (or large root heap) to select the small (or maximum) number of key words in the current unordered region of the sorting. We might as well use a large heap to sort. The basic operation of each trip is to adjust the current unordered area to a large root heap, select the maximum heap top record for the keyword, and swap it with the last record in the unordered area. Thus, just as opposed to direct selection, the ordered region is formed at the end of the original record area and gradually expands to the entire recording area.
"Example": Build a heap on the keyword sequence 42,13,91,23,24,16,05,88.
5.4. Program implementation
<summary> Small Gan Sort </summary> <param name= "Dblarray" ></param> <param name= "StartIndex" ></param> <returns></returns>
private static void Heapsort (ref double[] Dblarray) { for (int i = dblarray.length-1 i >= 0; i--) { if (2*i+1<dblarray.length) { int minchildrenindex = 2*i+1; Compare Saozi to the right subtree and record the index of the minimum value if (2*i+2 < dblarray.length) { if (dblarray[2*i+1]>dblarray[2*i+2]) Minchildrenindex = 2*i+2; } if (Dblarray[i] > Dblarray[minchildrenindex]) { Exchagevalue (ref dblarray[i],ref Dblarray[minchildrenindex]); Nodesort (ref dblarray, Minchildrenindex); } } } }
<summary> Node sorting </summary> <param name= "Dblarray" ></param> <param name= "StartIndex" ></param>
private static void Nodesort (ref double[] Dblarray,int StartIndex) { while (2*startindex+1 < dblarray.length) { int minchildrenindex = 2*startindex+1; if (2*startindex+2 < dblarray.length) { if (dblarray[2*startindex+1]>dblarray[2*startindex+2]) { Minchildrenindex = 2*startindex+2; } } if (Dblarray[startindex] > Dblarray[minchildrenindex]) { Exchagevalue (ref dblarray[startindex],ref Dblarray[minchildrenindex]); StartIndex = Minchildrenindex; } } }
<summary> Exchange value </summary> <param name= "A" ></param> <param name= "B" ></param>
private static void Exchagevalue (ref double A, ref double B) { Double Temp = A; A = B; B = Temp; } |