Original: Step by step Write algorithm (heap sort)
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Heap sorting is another common recursive sort. Because heap sorting has excellent sorting performance, it is often used in software design. Heap ordering has its own special properties, and the two-fork balance tree is basically consistent. For example, every data in a large heap must satisfy such a feature:
(1) Each array[n] not less than array[2*n]
(2) Each array[n] not less than array[2 * n + 1]
Building such a heap is just the foundation, and we need to get rid of the data from the top of the heap each time, adjusting the heap until the array becomes an ordered array. So the detailed heap sorting algorithm should be this:
1) Build a large heap so that each data in the heap satisfies the properties mentioned above
2) Swap the first data of the heap with the last data of the heap, and then readjust the heap until the heap is rebalanced
3) Repeat the process of 2) until the entire array is ordered.
The above description process is very simple, so what is the practical operation?
a) to determine the parameters of the entry
void Heap_sort (int array[], int length) {if (NULL = = Array | | 0 = = length) return;/* To do sure data starts at number 1 */ _heap_sort (array-1, length);}
b) building large piles and adjusting large piles
void _heap_sort (int array[], int length) {int index = 0;int median = 0;construct_big_heap (array, length); for (index = length ; Index > 1; Index--) {median = array[1];array[1] = Array[index];array[index] = median;reconstruct_heap (array, 1, index-1);}}
c) Build a large heap of detail operation parts
void Set_sorted_value (int array[], int length) {int index = Length;int median = 0;if (length = = 1) return;while (Index > 1 {if (Array[index >> 1] >= array[index]) Break;median = Array[index];array[index] = Array[index >> 1];array [index >> 1] = Median;index >>= 1;}} void construct_big_heap (int array[], int length) {int index = 0; for (index = 1; index <= length; index + +) {Set_sorted_va Lue (array, index);}}
d) Large stack of iterative adjustments
void reconstruct_heap (int array[], int index, int length) {int swap = 0;if (Length < index << 1) return;if (length = = Index << 1) {adjust_leaf_position (array, index); return;} if ( -1! = (Swap = adjust_normal_position (array, index))) {reconstruct_heap (array, swap, length);}}
e) The single branch node and the full point node are processed separately
int adjust_normal_position (int array[], int index) {int left = index << 1; int. = left + 1;int median = 0;int SW AP = 0;if (Array[index] >= Array[left]) {if (Array[index] >= array[right]) {return-1;} Else{swap = Right;}} Else{if (Array[index] >= array[right]) {swap = left;} Else{swap = Array[left] > array[right]? left:right;}} if (swap = = left) {median = Array[index];array[index] = Array[left];array[left] = median;} Else{median = Array[index];array[index] = array[right];array[right] = median;} return swap;} STATUS adjust_leaf_position (int array[], int index) {int median = 0;if (Array[index] > Array[index << 1]) return TRU E;median = Array[index];array[index] = Array[index << 1];array[index << 1] = Median;return FALSE;}
f) Heap sorting algorithm is complete, create test case validation
static void Test1 () {int array[] = {1};heap_sort (array, sizeof (array)/sizeof (int));} static void Test2 () {int array[] = {2, 1};heap_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), assert (2 = = AR RAY[1]);} static void Test3 () {int array[] = {3, 2, 1};heap_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), assert (2 = = ARRAY[1]); assert (3 = = array[2]);} static void Test4 () {int array[] = {2, 3, 1};heap_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), assert (2 = = ARRAY[1]); assert (3 = = array[2]);} static void Test5 () {int array[] = {5,3, 4, 1};heap_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), ASSERT (3 = = Array[1]); assert (4 = = array[2]); assert (5 = = array[3]);} static void Test6 () {int array[] = {2, 3,6, 8, 7};heap_sort (array, sizeof (array)/sizeof (int)), assert (2 = = Array[0]), assert (3 = = array[1]), assert (6 = = array[2]), assert (7 = = Array[3]), assert (8 = = array[4]);} static void Test7 () {int array[] = {3,4,2,7,1,9,8,6,5};heap_sort (array, sizeof (array)/sizeof (inT), assert (1 = = Array[0]), assert (2 = = array[1]), assert (3 = = array[2]), assert (4 = = Array[3]), assert (5 = = array[4]); assert (6 = = Array[5]), assert (7 = = Array[6]), assert (8 = = Array[7]), assert (9 = = Array[8]);}
"preview: The following blog describes some of the commonly used data structures"
Step-by-step write algorithm (heap sort)