堆排序
发布于 2022-03-03 17:06:35
函数heap_sort使用堆排序方法对数组arr进行排序,排序后数据为降序。相关代码如下,请补充缺失部分。
void heap_arrange(int arr[], int cur, int cnt) //调整为小顶堆
{
int heaptop_val = arr[cur] //堆顶的值
while (cur < cnt) {
int left = 2 * cur + 1
int right = 2 * cur + 2
int min = -1
int min_val = ______
if (left < cnt && arr[left] < min_val) { //检查是否比左节点大
min = left
min_val = arr[left]
}
if (right < cnt && arr[right] < min_val) {//检查是否比右节点大
min = right
}
if (min == ______)
break
arr[cur] = ______
cur = ______
}
arr[cur] = ______
}
void heap_sort(int arr[], int cnt)
{
int i
for (i = cnt / 2 - 1 i >= 0 --i) {
heap_arrange(arr, i, cnt)
}
for (i = cnt - 1 i > 0 --i) {
int tmp
tmp = arr[0]
arr[0] = arr[i]
arr[i] = tmp
heap_arrange(arr, 0, i)
}
}
关注者
0
被浏览
22