堆排序c++实现
//大堆比较
template<class T>
bool max_compare(T a, T b)
{
return a < b;
}
//小堆比较
template<class T>
bool min_compare(T a, T b)
{
return a > b;
}
template<class T>
void heapify(T *src, int start,int len,bool (*compare)(T ,T ))
{
int left = (start + 1)*2 - 1;
int right = left + 1;
int temp = start;
while (true)
{
if (left<len && compare(src[temp] , src[left]))
temp = left;
if (right<len && compare(src[temp] , src[right]))
temp = right;
if (start == temp)
break;
swap(src[start], src[temp]);
start = temp;
left = (start+1)*2 - 1;
right = left + 1;