Heap Sort Algorithm

heap

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/

Building Heap from Array

Given an array of N elements. The task is to build a Binary Heap from the given array. The heap can be either Max Heap or Min Heap.

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.
要从上述给定的数组元素中建立最大堆,可以清楚地看到,上述形成的完整二叉树并不遵循堆属性。因此,我们的想法是按照自上而下的方法,将数组形成的完整二叉树按相反的层级顺序进行堆化。即首先堆化树中按层级顺序遍历的最后一个节点,然后堆化倒数第二个节点,以此类推。

Array = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17}
Corresponding Complete Binary Tree is:

             1
          /     \
       3         5
    /    \      /   \
  4       6    13   10
 / \     / \    
9   8   15 17

The task to build a Max-Heap from above array.

Total Nodes = 11.
Last Non-leaf node index = (11/2) – 1 = 4.
Therefore, last non-leaf node = 6.

To build the heap, heapify only the nodes: [1, 3, 5, 4, 6] in reverse order.

Heapify 6: Swap 6 and 17.

             1
          /     \
       3         5
    /    \      /  \
  4       17  13   10
 / \     /  \  
9   8  15    6

Heapify 4: Swap 4 and 9.

             1
          /     \
       3         5
     /    \    /   \
   9      17   13   10
  / \    /  \   
 4   8  15   6

Heapify 5: Swap 13 and 5.

             1
          /     \
       3         13
    /    \      /  \
  9      17   5    10
 / \    /  \  
4   8  15   6

Heapify 3: First Swap 3 and 17, again swap 3 and 15.

             1
         /     \
      17         13
   /    \       /  \
  9      15    5   10
 / \    /  \
4   8  3    6

Heapify 1: First Swap 1 and 17, again swap 1 and 15, finally swap 1 and 6.

             17
          /      \
       15         13
     /    \      /  \
    9      6    5   10
   / \    /  \
  4   8  3    1

Below is the implementation of the above approach:

英语:

Last non-leaf node 最后一个非叶子节点
the second last node 倒数第二个节点

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

  • 23
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值