/*************************************** *********************
Function: heap sorting
Author: glq2000 [glq2000@126.com]
Date: Tues, 2010-8-3
Note:
Pay attention to two issues when writing the heap sorting Function
1. How to Create a heap from an unordered sequence?
2. How can I adjust the remaining element to become a new heap after the top element of the output heap?
**************************************** *********************/
# Include <iostream>
# Include <cstring>
Using namespace std;
Void BigHeapAdjust (int * p, int r, int len); // Filter Function
Void BigHeapSort (int * p, int len); // sorting function of the top heap
Void SmallHeapAdjust (int * p, int r, int len); // Filter Function
Void SmallHeapSort (int * p, int len); // The sorting function of the small top heap.
Int main ()
{
Int array [100] = {0}; // used to hold elements to be sorted
Int n; // The number of elements to be sorted. The value cannot exceed 100.
Scanf ("% d", & n );
For (int I = 0; I <n; ++ I)
Scanf ("% d", array + I );
BigHeapSort (array, n); // sort the elements in the array from small to large.
// SmallHeapSort (array, n); // sorts the elements in the sorted array from large to small.
For (int K = 0; k <n; ++ K)
Printf ("% d", array [k]);
Getchar ();
Getchar ();
Getchar ();
Return 0;
}
/*************************************** *****************************
Big Top heap filter function:
P: array to be sorted
R: subscript of the element to be adjusted. Note that the subscript of the nth element in the array is n-1.
Len: Number of array elements
**************************************** ****************************/
Void bigheapadjust (int * P, int R, int Len)
{
Int TMP = P [R];
Int J;
For (j = 2 * r + 1; j <= len-1; j = 2 * j + 1) // filter the son with a large node value down the layer, 2 * r + 1 is the left son, 2 * (R + 1) is the right son
{
If (j <len-1 & P [J + 1]> = P [J]) // Because j <len-1, P [J + 1] does not cross the border
+ + J; // if the right son is greater than the left son, J ++ is transferred to the right son, so that J is equal to the big one in the left and right sons.
If (TMP> = P [J])
Break;
P [R] = P [J]; // a large son pan to the parent node and updates the location of the r node.
R = J;
}
P [R] = TMP; // place the root node in the appropriate position of the final blank
}
/*******************************
Big Top heap sorting function:
P: array to be sorted
Len: Number of array elements
********************************/
Void bigheapsort (int * P, int Len)
{
Int I, J;
// For A Complete Binary Tree, a total of Len/2 Non-leaf nodes, and the element with the subscript Len/2-1 is the last non-leaf node.
// Start from the last non-leaf node in reverse order and use the heapadjust function to adjust the size of the heap so that each root node contains the largest key.
For (I = Len/2-1; I> = 0; -- I)
Bigheapadjust (P, I, Len );
P [0] ^ = P [len-1];
P [len-1] ^ = p [0];
P [0] ^ = p [len-1]; // The above is the switching heap top and heap bottom, the following begins to adjust once, switch once
For (j = len-1; j> 1; -- j) // at the beginning of this write j> 0, in fact j> 1 can; because j = 2, after HeapAdjust is adjusted and switched, the entire array is ordered.
{
BigHeapAdjust (p, 0, j );
// Swap heap top and heap bottom
P [0] ^ = p [J-1];
P [J-1] ^ = p [0];
P [0] ^ = p [J-1];
}
}
/*************************************** ****************************
Small top heap filtering function:
P: array to be sorted
R: subscript of the element to be adjusted. Note that the subscript of the nth element in the array is n-1.
Len: Number of array elements
**************************************** ****************************/
Void SmallHeapAdjust (int * p, int r, int len)
{
Int tmp = p [r];
Int j;
For (j = 2 * r + 1; j <= len-1; j = 2 * j + 1) // filter son with smaller node values down the layer, 2 * r + 1 is the left son, 2 * (r + 1) is the right son
{
If (j <len-1 & p [j + 1] <= p [j]) // Because j <len-1, p [j + 1] does not cross the border
+ + J; // if the right son is less than the left son, then j ++ transfers to the right son to make j equal to the small one in the left and right sons.
If (tmp <= p [j])
Break;
P [r] = p [j]; // a smaller son translates the child to the parent node and updates the location of the r node.
R = j;
}
P [r] = tmp; // place the root node in the appropriate position of the final blank
}
/*******************************
Small top heap sorting function:
P: array to be sorted
Len: Number of array elements
********************************/
Void SmallHeapSort (int * p, int len)
{
Int I, j;
// For A Complete Binary Tree, a total of len/2 Non-leaf nodes, and the element with the subscript len/2-1 is the last non-leaf node.
// Start from the last non-leaf node in reverse order and use the HeapAdjust function to adjust the size of the heap so that each root node contains the largest key.
For (I = len/2-1; I> = 0; -- I)
SmallHeapAdjust (p, I, len );
P [0] ^ = p [len-1];
P [len-1] ^ = p [0];
P [0] ^ = p [len-1]; // The above is the switching heap top and heap bottom, the following begins to adjust once, switch once
For (j = len-1; j> 1; -- j) // adjust from the root, swap heap and heap bottom after Tuning
{
SmallHeapAdjust (p, 0, j );
// Swap heap top and heap bottom
P [0] ^ = p [J-1];
P [J-1] ^ = p [0];
P [0] ^ = p [J-1];
}
}