## What is Heap?

A Heap is a type of data structure that is built on trees. It's a binary tree that's virtually complete. Except for the very bottom level, all levels of the tree must be filled in a heap. The last (bottom) level should be filled from left to right. The heap data structure is used for the efficient implementation of priority queues.
Heap can be of two types:

1. Min Heap
2. Max Heap

In the case of min-heap, the root element is the minimum and it is maximum in the case of max heap. The number of edges between the root node and a leaf node at the last level of the tree is the height of the heap.

### Min Heap

Min-Heap is a type of heap in which the value of the Parent (P) node is less than or equal to that of all of its children (C). The root node in min-heap is the smallest amongst all its children nodes across the tree.

The heap is constructed by using the property of the key as

$Key\left(P\right)\le Key\left(C\right)$

In the following Min heap, the key value of the parent is less than the key value of all of its children nodes.

### Max Heap

Max Heap is a type of heap in which the value of the Parent (P) node is greater than or equal to either of its children (C) is known as Max Heap. The root node in the max heap is greatest amongst all its children nodes across the tree.

The heap was constructed by using the property of the key as

$Key\left(P\right)\ge Key\left(C\right)$

In the following Max heap, The parent's key value is higher than the key value of the children's nodes.

## What is Heapsort?

Heapsort is an efficient and popular sorting algorithm out of many sorting algorithms. It works with the concept of elimination and a function called heapify. Heapsort is the in-place sorting algorithm which means the elements from the heap part of the list are eliminated one by one and are then inserted into the sorted part of the list.

## How does the Heap sort works?

Heapsort makes at least n−1 comparisons to find the highest element in an array A[0,n], but we aim to minimize the number of elements that are compared directly to it. Heapsort uses the Heapify function to sort the data structure. Heapify is a very common operation in heap sort which rearranges the heap to maintain its property. On every operation, all element in the heap is compared and/or repositioned.

Two main phases should be taken into consideration while sorting the elements using the heapsort algorithm, they are as follows:

1. To begin, modify the array items to begin constructing the heap.
2. After the heap has been built, remove the root element by moving it to the end of the array, and then save the heap structure with the remaining items.

### Let us try to comprehend this in greater depth

If an array in memory has N distinct elements, the heapsort algorithm operates as follows:

1. To begin, a heap is constructed by repositioning the components within the array to their respective positions. This means that as the elements are visited from the array's root, the left and right child trees are filled in, forming a binary tree.
2. The element at the root node is eliminated from the heap in the second phase by shifting it to the end of the array.
3. It's possible that the balance elements aren't a heap. For the balancing elements, steps 1 and 2 are repeated. The process is repeated until all of the elements have been removed.

When we remove an element from the heap, we must reduce the array's maximum index value by one. For a max-heap, the elements are eliminated in decreasing order, while for a min-heap, the components are eliminated in increasing order.

## Heap sort algorithm

The major steps involved in the heapsort algorithm are given as follows. These procedures work for the max-heap property.

1. Building a heap by using MAX_HEAPBUILD(A) procedure.
2. Maintaining heap property by using the procedure MaxHeapify(A)

### Building a heap

To implement the heapify property of the binary tree, a heap is created and use the MaxHeapify procedure to arrange the nodes as a Max heap. The procedure begins at the bottom of the tree although, in order to run MAXHEAPIFY on a node, its subtrees must already be heaps. The procedure is given below:

$MAX_HEAPBUILD\left(A\right)$

1. $A.heapsize=A.length$
2. $MAXHEAPIFY\left(A,i\right)$

### Maintaining heap property

The MAXHEAPIFY procedure attempts to "heapify" the subtree rooted at i given a heap A and a node i within that heap. It rearranges the nodes in A, causing the subtree at node i to satisfy the max-heap property. The procedure is given below:

$MAXHEAPIFY\left(A,i\right)$

1. $l=LEFT\left(i\right)$
2. $r=RIGHT\left(i\right)$
3. $largest=l$
4. $largest=r$
5. $MAXHEAPIFY\left(A,l\right)$

Now, we can implement the heapsort algorithm by using the above implementations.

### Algorithm

$HEAPSORT\left(A\right)$

1. $MAX_HEAPBUILD\left(A\right)$
2. $A.heapsize=A.heapsize-1$
3. $MAXHEAPIFY\left(A,1\right)$

## Complexity

Heapify is the central operation in heapsort. Examine the height log n of an n-element heap. In addition, each n-element heap has a maximum of

$n}{{2}^{h+1}}$

any height nodes h. So, The MAX HEAPBUILD(A) procedure has an O(n) execution time.

Because the heap's height is O(log n), the procedure MAXHEAPIFY takes O(log n). As a result, the heapsort's overall running time is O(n log n).

## Common Mistakes

Although quicksort is faster than heap sort, it performs worse on large data sets. Quicksort is best suited for lists with few elements and gives fast results, unlike heapsort, which is slower but runs more smoothly on large data sets. Heapsort can be helpful in a real system where memory usage is limited.

## Context and Applications

This topic is significant in the competitive and professional exams for graduate and postgraduate courses, especially for:

• B. Tech in Computer Science
• M. tech in Computer Science
• Master of Computer Applications

• Types of sorting
• Priority queue
• Binary Tree
• Quicksort
• Heapify

## Practice Problems

Q1. Heap Sort is based on which algorithm?

1. Binary tree
2. FIFO
3. LIFO
4. Priority queue

Explanation: The element with the highest priority is served first in the priority queue, followed by the element with the lowest priority. This property is used to implement the heapify heapsort property.

Q2. The following is a binary tree in which the parent node is greater than the child node:

1. min-heap
2. heapify
3. max-heap
4. complete binary tree

Explanation: A heap is a binary tree that can be constructed in one of two ways. The first is the Max heap, and the second is the Min heap. The parent node in a max heap has a higher value than the child nodes.

Q3. How long does it take to assemble a heap of elements?

1. $O\left(n\mathrm{log}\left(n\right)\right)$
2. $O\left(\mathrm{log}\left({n}^{2}\right)\right)$
3. $O\left(n\right)$
4. $O\left({n}^{3}\right)$

Correct Answer: c. $O\left(n\right)$

Explanation: The fundamental method is to construct a binary heap of n elements in $O\left(n\right)$ time.

Q4. The following is a binary tree in which the parent node is smaller than the child node:

1. min-heap
2. heapify
3. max-heap
4. Both min-heap and max-heap

Explanation: A heap is a binary tree that can be constructed in two ways. The first is the Max heap, and the second is the Min heap. The parent node in a Min heap has a lower value than the child nodes.

Q5. Heapify function is used in which data structure?

1. Selection sort
2. Heap sort
3. Bubble sort
4. None

Explanation: The heapify function is used in heap sort to arrange the elements in the array to order the left and right subtree elements. This function is only used in the heap data structure.

### Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

### Heapsort

• Copyright, Community Guidelines, DSA & other Legal Resources: Learneo Legal Center
• bartleby, a Learneo, Inc. business