【自我笔记之数据结构】排序---堆排序
作者:互联网
void HeapAdjust(int arr[], int pos, int len)
{
int i = pos;
int tmp = 0;
for(int j = 2*i+1; j < len; j = 2*i+1)
{
if(j < len-1 && arr[j+1] > arr[j])
j++;
if(arr[i] >= arr[j])
break;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i = j;
}
}
void HeapSort(int arr[], int len)
{
int tmp = 0;
for(int i = len/2 - 1; i>= 0; --i)
HeapAdjust(arr, i, len);
for(int j = len-1; j > 0; --j)
{
tmp = arr[0];
arr[0] = arr[j];
arr[j] = tmp;
HeapAdjust(arr, 0, j);
}
}
标签:tmp,arr,排序,int,void,堆排序,len,HeapAdjust,数据结构 来源: https://blog.csdn.net/weixin_41576955/article/details/98438525