Heap Sort Algorithm

A heap is a specialized tree-based data structure that satisfies the heap property. It’s commonly implemented as a binary tree, particularly an array-based representation, where the parent node’s value is either greater than or less than (depending on whether it’s a max heap or min heap) the values of its children.

Complete binary trees: A binary tree in which all levels are completely filled except the last one. And if the last level is filled partially, it should be filled from left to right.

Key Characteristics:

  1. Heap Property: There are two types of heaps based on the heap property:

    • Max Heap: Every parent node has a value greater than or equal to the values of its children. A max-heap supports O(log n) insertions, O(1) time lookup for the max element, and O(log n) deletion of the max element. Searching for arbitrary keys has O(n) time
    • Min Heap: Every parent node has a value less than or equal to the values of its children. it supports O(1) time lookups for the minimum element.
  2. Representation:

    • Array Representation: In many implementations, heaps are represented using arrays, where the relationship between parent and child nodes is determined by their indices within the array.
      the children of the node at index i are at indices 2i + 1 and 2i + 2
  3. Common Operations:

    • Insertion: Adding a new element while maintaining the heap property.
    • Deletion: Removing the root node (max or min) while maintaining the heap property.
    • Heapify: Ensuring that the heap property is maintained after insertion or deletion.

Types of Heaps:

  1. Binary Heap: The most common type of heap, where each parent node has at most two children. It’s efficient for priority queue implementations and is often used in algorithms like Heap Sort and Dijkstra’s shortest path algorithm.

  2. Fibonacci Heap: A more advanced heap data structure that has better amortized time complexity for some operations compared to binary heaps, especially in certain graph algorithms.

Heap Applications:

  • Priority Queues: Heaps are often used to implement priority queues due to their ability to efficiently retrieve and remove the maximum or minimum element.
  • Heap Sort: A sorting algorithm that uses a heap data structure to sort elements in-place.
  • Dijkstra’s Algorithm: Finds the shortest path in a graph using a priority queue implemented with a heap.

Example of a Max Heap (Array Representation):

Let’s consider a simple max heap:

               9/   \7     6/ \   / 5   4 3  

The array representation of this heap would be [9, 7, 6, 5, 4, 3], where each index represents a node, and the relationship between parent and child nodes is maintained based on their indices.

Heaps are efficient data structures that facilitate operations requiring quick access to the maximum or minimum element, making them valuable in various algorithms and applications

Heap Sort Algorithm

To solve the problem follow the below idea:
First convert the array into heap data structure using heapify, then one by one delete the root node of the Max-heap and replace it with the last node in the heap and then heapify the root of the heap. Repeat this process until size of heap is greater than 1.

  1. Build a heap from the given input array.
  2. Repeat the following steps until the heap contains only one element:
    i. Swap the root element of the heap (which is the largest element) with the last element of the heap.
    ii. Remove the last element of the heap (which is now in the correct position).
    iii. Heapify the remaining elements of the heap.
  3. The sorted array is obtained by reversing the order of the elements in the input array.

https://www.geeksforgeeks.org/building-heap-from-array/
https://www.geeksforgeeks.org/heap-sort/

input: arr[] = {4, 10, 3, 5, 1}

       4/   \10     3/  \
5     1

Output: Corresponding Max-Heap:

       10/   \5     3/  \
4      1

Note:

  1. Root is at index 0 in array.
  2. Left child of i-th node is at (2*i +1)th index.
  3. Right child of i-th node is at (2*i + 2)th index.
  4. Parent of i-th node is at (i-1)/2 index.

Naive Approach: To solve the problem follow the below idea:

To build a Max-Heap from the above-given array elements, It can be clearly seen that the above complete binary tree formed does not follow the Heap property. So, the idea is to heapify the complete binary tree formed from the array in reverse level order following a top-down approach. That is first heapify, the last node in level order traversal of the tree, then heapify the second last node and so on.
要从上述给定的数组元素中建立最大堆,可以清楚地看到,上述形成的完整二叉树并不遵循堆属性。因此,我们的想法是按照自上而下的方法,将数组形成的完整二叉树按相反的层级顺序进行堆化。即首先堆化树中按层级顺序遍历的最后一个节点,然后堆化倒数第二个节点,以此类推。

英语:
Last non-leaf node 最后一个非叶子节点

https://qidawu.github.io/posts/data-structure-tree/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/221813.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IncDec序列

title: IncDec序列 date: 2023-12-14 21:10:36 tags: 差分 categories: 算法进阶指南 —>传送门 题目大意 解题思路 区间操作,可以考虑差分。观察发现,最终变成相同的数,相当于相邻的两个数之差为 0 0 0,因此我们使用差分。先…

快乐数(力扣

bool isHappy(int n) {int num10,temp;while(num--){temp0;while(n){temppow(n%10,2);n/10;}ntemp;if(n1) return true;}return false; }

35、卷积算法总结

前面大概花了 10 几节的篇幅,介绍了卷积这一算法,我觉得是值得的。 因为在CNN网络中,卷积是属于绝对的核心,而且围绕着卷积可衍生出来的算法还有很多,并且卷积的算法思想在很多其他算法中都有体现,比如矩阵乘法、全连接算法。这一节来总结一下这个算法。 卷积算法的参数…

hash累计上文

import hashlib def gen_hash(text):# 创建一个SHA256哈希对象hash_object hashlib.sha256()# 更新哈希对象hash_object.update(text.encode())# 获取哈希值(十六进制字符串)hex_dig hash_object.hexdigest()return hex_dig# 要计算哈希值的文本 data …

【开题报告】基于SpringBoot的办公消耗品管理系统的设计与实现

1.研究背景 办公消耗品管理系统的设计与实现,是针对当前企业、公司等办公场所对于各种消耗品进行高效管理的需求而提出的。随着企业规模的扩大和办公自动化程度的提高,传统的手工管理已经无法满足日益增长的管理需求。因此,基于SpringBoot的…

.[henderson@cock.li].Devos、[myers@airmail.cc].Devos勒索病毒的最新威胁:如何恢复您的数据?

尊敬的读者: .[hendersoncock.li].Devos、[myersairmail.cc].Devos 勒索病毒的威胁无处不在。通过深入了解其特征、有效的数据恢复方法以及全面的预防措施,我们可以更好地保护自己、家人和企业的数字财产。在网络的战场上,谨慎和预防是我们最…

c语言快速排序(霍尔法、挖坑法、双指针法)图文详解

快速排序介绍: 快速排序是一种非常常用的排序方法,它在1962由C. A. R. Hoare(霍尔)提的一种二叉树结构的交换排序方法,故因此它又被称为霍尔划分,它基于分治的思想,所以整体思路是递归进行的。 …

vite 中使用插件 vite-plugin-svg-icon加载svg资源

1.安装 pnpm i vite-plugin-svg-icons -D2.配置 vite 在vite.config.ts 中配置 import { createSvgIconsPlugin } from vite-plugin-svg-icons; import path from path;function configSvgIconsPlugin(isBuild: boolean) {const svgIconsPlugin createSvgIconsPlugin({// 要…

不会设计Logo,不如来看看这9个Logo设计软件!

优秀的 Logo 是企业的形象展示。大公司将花费数万甚至数百万定制特色标志。由于预算有限,一些中小企业或企业家往往希望找到更具成本效益的渠道。这里有 9 个免费的 Logo 设计软件。用户可以通过输入品牌名称来生成自己的 Logo。 1、即时设计 即时设计是一个国内的…

Tomcat部署(图片和HTML等)静态资源时遇到的问题

文章目录 Tomcat部署静态资源问题图中HTML代码启动Tomcat后先确认Tomcat是否启动成功 Tomcat部署静态资源问题 今天,有人突然跟我提到,使用nginx部署静态资源,如图片。可以直接通过url地址访问,为什么他的Tomcat不能通过这样的方…

leetcode 904. 水果成篮(优质解法)

代码: class Solution {public int totalFruit(int[] fruits) {int lengthfruits.length;int []fruitNumsnew int[length1]; //用于记录各个种类摘了多少个水果int count0; //用于记录当前采摘了几种水果int sum0; //用于记录当前共摘了多少水果for(int left0…

Peter算法小课堂—简单建模(2)

太戈编程736题 题目描述: 你是一只汪星人,地球毁灭后你回到了汪星,这里每天有n个小时,你需要为自己选择正好连续的m小时作为每天睡眠的时间。从凌晨开始,第i小时内的睡眠质量为xi,请问经过选择后&#xf…

详解—C++右值引用

目录 一、右值引用概念 二、 左值与右值 三、引用与右值引用比较 四、值的形式返回对象的缺陷 五、移动语义 六、右值引用引用左值 七、完美转发 八、右值引用作用 一、右值引用概念 C98中提出了引用的概念,引用即别名,引用变量与其引用实体公共…

Android P 9.0 增加以太网静态IP功能

效果图 一、Settings添加以太网的配置&#xff1a; 1、vendor\mediatek\proprietary\packages\apps\MtkSettings\res\xml\network_and_internet.xml <com.android.settingslib.RestrictedPreferenceandroid:key"ethernet_settings"android:title"string/et…

如何在PHP中发送电子邮件?

在 PHP 中发送电子邮件通常涉及使用内置的 mail 函数或使用更强大的邮件库&#xff0c;例如 PHPMailer 或 Swift Mailer。以下是使用 mail 函数和 PHPMailer 的基本示例&#xff1a; 使用 mail 函数&#xff1a; <?php$to "recipientexample.com"; $subject …

集合04 Collection (Set) - Java

Set Set 基本介绍Set 常用方法Set 遍历方式 HashSet 的全面说明练习 HashSet 的底层机制说明HashSet 的扩容机制&转成红黑树机制练习1练习2 LinkedHashSetLinkedHashSet底层源码练习 Set 基本介绍 无序&#xff08;添加和取出的顺序不一致)&#xff0c;没有索引 [后面演示…

【Java系列】详解多线程(二)——Thread类及常见方法(下篇)

个人主页&#xff1a;兜里有颗棉花糖 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 兜里有颗棉花糖 原创 收录于专栏【Java系列专栏】【JaveEE学习专栏】 本专栏旨在分享学习Java的一点学习心得&#xff0c;欢迎大家在评论区交流讨论&#x1f48c; 目录 一…

大模型微调的“温度”参数,原来影响的是 softmax

大家好啊&#xff0c;我是董董灿。 在对大模型进行微调训练时&#xff0c;经常会看到几个重要的超参数&#xff0c;用来控制大模型生成文本的效果。 其中一个超参数叫做 Temperature&#xff0c;中文名字叫温度&#xff0c;初见时很是不解&#xff0c;为啥一个模型还有温度这个…

将创建表字段语句快速转换成golang struct字段

用网页jquery快速生成 本地建立 struct.html <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>leo-转换</title> <script src"https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></s…

深入学习《大学计算机》系列之第1章 1.2节——问题描述与抽象

一.欢迎来到我的酒馆 第1章 1.2节&#xff0c;问题描述与抽象。 目录 一.欢迎来到我的酒馆二.问题描述、抽象与建模1.什么是抽象2.为什么要抽象3.什么是建模 二.问题描述、抽象与建模 什么是抽象&#xff1f;为什么要抽象&#xff1f;什么是建模&#xff1f;建什么模&#xff1…