问题描述
Heaps的主要应用之一是Heap-Sort。它由2个步骤组成
(1)构建堆和
(2)通过重复删除最大元素来创建排序数组(每次删除后更新堆以维护堆)。
输入
N
x1 x2 ......... xn
其中,
N是要排序的总数
xi输入数字
输出
y1 y2 ......... yn
其中数组y已分类
堆分类功能已经可供您使用
您需要完成构建堆和堆化功能
您需要编写以下函数代码
//函数使用maxHeapify创建和构建堆
void createHeap(struct MaxHeap * maxHeap){
//单个节点是一个堆
//从最底层和最右边的内部节点开始,以自下而上的方式堆叠所有内部节点
}
//堆积最大堆。
void heapify(struct MaxHeap * maxHeap,int idx){
// idx是要堆积的节点的索引
}
One of the main application of Heaps is Heap-Sort. It consists of 2 steps
(1) Build the heap and
(2) Sorted array is created by repeatedly removing the largest element (Heap is updated after each removal to maintain the heap).
Input
N
x1 x2 ……… xn
where,
N is the total numbers to be sorted
xi input numbers
Output
y1 y2 ……… yn
where array y is sorted
Heap sort function is already made available to you
You need to complete the build heap and heapify function
You need to code the following functions
// function creates and build a heap using maxHeapify
void createHeap(struct MaxHeap* maxHeap){
// Single Node is a heap
// Start from bottom most and rightmost internal node and heapify all internal nodes in bottom up way
}
// heapify a max Heap.
void heapify(struct MaxHeap* maxHeap, int idx){
// idx is the index of the node you want to heapify
}
推荐答案
这篇关于堆排序程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!