堆排序程序 [英] Program for Heap sort

查看:117
本文介绍了堆排序程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
}

推荐答案

你必须做自己的功课。此外,谷歌搜索你可能会发现许多堆排序教程(甚至实现)。您的起点可能是 Heapsort维基百科页面 [ ^ ]。
You have to do your own homework. Moreover just Googling you may find many many heap sort tutorials (even implementations). You starting point could be the Heapsort Wikipedia page[^].


这篇关于堆排序程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆