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/

你可能感兴趣的:(程序员英语面试,算法)