C # Heap Ordering
Using System; Using System.Collections; Namespace sort {public class Heapsorter {public static int[] Sort (int[] sortarray) { Buildmaxheap (Sortarray); for (int i = (sortarray.length-1); i > 0; i--) {Swap (ref sortarray[0], ref sortarray[i ]); Exchange Maxheapify (sortarray, 0, I) with the last element of the heap top element and the unordered area; The new unordered area is resized to a heap, and the unordered area is smaller} return sortarray; }///<summary>////The initial large heap, build heap from bottom up///Complete binary tree basic properties, the bottom node is N/2, so start with SORTARRAY.LENGTH/2 </summary> private static void Buildmaxheap (int[] sortarray) {for (int i = (SORTARRAY.LENGTH/2)-1; I >= 0; i--) {maxheapify (sortarray,i, sortarray.length); }}///<summary>//Adjust the specified node to heap///</summary>//<param Nam E= "I"Nodes that need to be adjusted </param>///<param Name= "HeapSize" > Heap size, also the length of unordered extents in the exponent group </param> private static VO ID maxheapify (int[] sortarray, int i, int heapsize) {int left = 2 * i + 1;//left Dial hand node int right = 2 * i + 2; Right child node int larger = i; Temporary variables, storing large node values//comparing left child nodes if (Ieft < heapsize && Sortarray[left] > Sortarray[large R]) {larger = left; }//Compare Right child node if (R < heapsize && Sortarray[right] > Sortarray[larger]) {larger = right; }//If a child node is larger than itself, the large element moves up. if (i! = larger) {Swap (ref sortarray[i], ref Sortarray[larger]); Maxheapify (Sortarray, larger, heapsize); }}//array element interchange private static void swap (ref int A, ref int b) {int t; t = A; A = b; b = t; } } }
The idea of heap sequencing:
The use of large top heap (small top heap) heap top record is the maximum keyword (minimum keyword) This feature, making it easy to select the maximum record (minimum record) from the disorder every time.
Its basic idea is (Big Top heap):
1) The initial order to sort the keyword sequence (r1,r2 .... Rn) is constructed into a large top heap, which is the initial disordered zone;
2) Swap the top element of the heap r[1] with the last element R[n] to get a new unordered area (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]<=r[n];
3) due to the new heap top r[1] may violate the nature of the heap, it is necessary to the current unordered zone (R1,R2,...... RN-1) adjusts to the new heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (R1,R2 ...). Rn-2) and the new ordered area (RN-1,RN). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is complete.
The operation process is as follows:
1) Initialize the heap: R[1..N] is constructed as a heap;
2) Swap the top element of the current unordered area R[1] with the last record of the interval, and then adjust the new unordered area to the new heap.
So for heap sequencing, the most important two operations are to construct the initial heap and adjust the heap, in fact, the construction of the initial heap is actually the process of adjusting the heap, but the initial heap is to construct all the non-leaf nodes are adjusted.
The above is the C # heap ordering content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!