Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

# Heap Sort Algorithm

The elements of a heap sort are processed by generating a min-heap or max-heap with the items of the provided array. Both heaps (max and min) help in describing the sequence of an array in which the root component or element reflects the array’s minimal or maximum element. In this article, we will learn more about heap sort algorithms.

Table of Contents

## Two Main Functions Recursively Performed by Heap Sort

• Create a heap H using the array’s elements.
• Remove the root component from the heap created in the first phase repeatedly.

Before learning more about the heap sort, it’s vital to understand more about heap.

### What is a Heap?

A heap is a full binary tree with a limit of two offspring (children) at each node.

A heap is a unique tree-based data structure that is functionally a complete tree and satisfies all the heap attributes. It is used in the field of computer science.

What is Heap Sort?

An efficient sorting algorithm is heapsort. The purpose of a heap sort is to shift items from the listing’s heap portion into the sorted section one at a time.

The Binary Heap data structure serves as the foundation for this comparison-based sorting method. The heapify method is frequently used in heapsort to arrange the items.

You can explore more about heap sort to get a better understanding of this topic.

## Working of Heap Sort Algorithm

When it comes to the heap sort, the elements are sorted largely in two steps, and they are as follows:

• The first step involves changing the array’s elements to create a heap.
• The next step is holding the heap structure with the left components while continuously extracting the heap’s root component by moving it to the end of the array.

We will use an example of an unsorted array to demonstrate how heap sort operates. We’ll attempt to sort this array in the following manner:

We will first generate a heap from the aforementioned array and turn it into a max heap.

The unsorted array is represented by the image on the left, while the maximum heap is shown on the tree’s right side. The array’s parts now have the following appearance:

The root component 88 from the maximum heap has to be eliminated next. Now, in order to eliminate this node, we must switch it with node 10 (the last node). We must heapify it once more to create the max heap after deleting the root component.

The array’s components are as follows after replacing component 88 with 10 and changing the heap into a max-heap:

The next step is to eliminate the root component 80 from the maximum heap. This node must be switched with the 53th node in order to be removed. To make it into the maximum heap, we must heapify it after eliminating the root component.

When the swapping procedure is complete, the array’s components will be 80 with 53 and the heap will be converted into a max-heap.

In order to proceed to the next step, we must eliminate the root component 75 from the maximum heap. This node must be replaced by the ninth node in order to be eliminated. Again we will heapify it to transform it into a max heap.

The components of the array are as follows after replacing the placement of array element 75 with 8 and turning the heap into a max-heap:

We must eliminate the root element 53 before moving on to the following step. To eliminate this node, we must relocate it to the last node, or position 13, in order to do so. We repeat the heapify process after deleting the root component.

The array’s components are as follows after replacing component 53 with component 13 and changing the heap into a max-heap:

The root element 21 needs to be eliminated right now. This node must be switched with the eleventh (last) node in order to be removed. We must heapify it in order to create the maximum heap after removing the root component.

The array’s components are as follows after replacing element 21 with number 10 and changing the heap into the maximum heap:

We must once more eliminate the root component 13 in the following step. This node must be moved into position next to node 9 in order to be removed.

The array’s components are as follows after replacing component 13 with component 8 and changing the heap into the maximum heap:

The root component 10 must be eliminated before we proceed to the following step. This node needs to be switched with component 8, which is the final one, in order to be eliminated. We must heapify the element once again in order to create the maximum heap after deleting the root element.

After swapping the array element 10 with 8, the components of the array are –

Now we have only one component left. After removing it, the heap will be empty.

After completion of the arrangement, the array components are –

Now, the array is fully organised or sorted.

## Heap Sort Complexity

Heap Sort complexity includes time and space complexity. Let’s walk through the table:

### 1. Time Complexity

 Case Time Complexity Best Case O(n logn) Average Case O(n log n) Worst Case O(n log n)

### 2. Space Complexity

 Space Complexity O(1)

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,