## 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:

- Min Heap
- 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:

- To begin, modify the array items to begin constructing the heap.
- 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:

- 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.
- The element at the root node is eliminated from the heap in the second phase by shifting it to the end of the array.
- 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.

- Building a heap by using MAX_HEAPBUILD(A) procedure.
- 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)$

- $A.heapsize=A.length$
- $Fori=\raisebox{1ex}{$A.length$}\!\left/ \!\raisebox{-1ex}{$2$}\right.downto1$
- $MAXHEAPIFY(A,i)$

### 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(A,i)$

- $l=LEFT\left(i\right)$
- $r=RIGHT\left(i\right)$
- $ifl\le A.heapsizeandA\left[l\right]A\left[i\right]$
- $largest=l$
- $Elselargest=i$
- $ifr\le A.heapsizeandA\left[r\right]A\left[largest\right]$
- $largest=r$
- $iflargest\ne i$
- $ExchangeA\left[i\right]withA\left[largest\right]$
- $MAXHEAPIFY(A,l)$

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

### Algorithm

$HEAPSORT\left(A\right)$

- $MAX\_HEAPBUILD\left(A\right)$
- $fori=A.lengthdownto2$
- $doexchangeA\left[1\right]withA\left[i\right]$
- $A.heapsize=A.heapsize-1$
- $MAXHEAPIFY(A,1)$

## 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

$\raisebox{1ex}{$n$}\!\left/ \!\raisebox{-1ex}{${2}^{h+1}$}\right.$

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

## Related Topics

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

## Practice Problems

**Q1.** Heap Sort is based on which algorithm?

- Binary tree
- FIFO
- LIFO
- Priority queue

**Correct Answer:** d. 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:

- min-heap
- heapify
- max-heap
- complete binary tree

**Correct Answer:** a. max-heap

**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?

- $O\left(n\mathrm{log}\left(n\right)\right)$
- $O\left(\mathrm{log}\left({n}^{2}\right)\right)$
- $O\left(n\right)$
- $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:

- min-heap
- heapify
- max-heap
- Both min-heap and max-heap

**Correct Answer:** a. min-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?

- Selection sort
- Heap sort
- Bubble sort
- None

**Correct Answer: **b. Heap sort

**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?

*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.

# Heap Sort Homework Questions from Fellow Students

Browse our recently answered Heap Sort homework questions.

### 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.