×

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find the higher element and add it at the end. We just repeat same thing again and again. In heap sort, we use binary heap data structure.

What is Binary Heap

A Binary heap is nothing but it is a Complete Binary Tree. In this, elements are stored in a different order such that parent node element is either greater or smaller than the value of its children nodes which is called max heap or min heap respectively. Binary Heap can be implemented by array and linked list and as we said binary heap is the complete binary tree so array is preferred for implementation.

Pseudo-code of Heap Sort

  • First, call build max heap to set the heap initially  
  • After the heap is created, take the root and add it as the last element of the heap
  • Decrease the size of the heap
  • Call max heapify function of index 0, i.e., the new root of the heap

Heap Sort Algorithm Dry Run

  • input:
01234
2310161120
  • The first step – we will make max heap with the help of function
01234
2320161110
  • For i=4
01234
2011161023
  • After i=3
01234
1611102023
  • After i=2
01234
1110162023
  • After i=1
01234
1011162023
  • After i=0
01234
1011162023

Heap Sort program in C-language

 #include <stdio.h>
   void swap(int *a, int *b) {
     int tmp = *a;
     *a = *b;
     *b = tmp;
   }
   void heapify(int array[], int n, int i) {
     int max = i;
     int left_Child = 2 * i + 1;
     int right_Child = 2 * i + 2;
     if (left_Child < n && array[left_Child] > array[max])
       max = left_Child;
     if (right_Child < n && array[right_Child] > array[max])
       max = right_Child;
     if (max != i) {
       swap(&array[i], &array[max]);
       heapify(array, n, max);
     }
   }
   void heap_Sort(int array[], int n) {
     for (int i = n / 2 - 1; i >= 0; i--)
       heapify(array, n, i);
     for (int i = n - 1; i >= 0; i--) {
       swap(&array[0], &array[i]);
       heapify(array, i, 0);
     }
   }
   void display(int array[], int n) {
     for (int i = 0; i < n; ++i)
       printf("%d ", array[i]);
     printf("\n");
   }
   int main() {
     int array[] = {11, 34, 9, 5, 16, 10};
     int n = sizeof(array) / sizeof(array[0]);
     printf("Original array:\n");
     display(array, n);
     heap_Sort(array, n);
     printf("Sorted array:\n");
     display(array, n);
   } 

Output of the program

  Original array:
  11 34 9 5 16 10
  Sorted array:
  5 9 10 11 16 34 

Heap Sort Time Complexity

  • Build max heap takes O(n/2) time
  • The Best Case Time Complexity: O(n log n)
  • The Average Case Time Complexity: O(n log n)
  • The Worst Case Time Complexity: O(n log n)

Related Topics

Sparse Matrix in Data Structure

Sparse Matrix The sparse matrix is a two-dimensional data object which is made by m rows and n columns, so we can say the number of data values in sparse matrix...

6 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

Binary Tree Inorder Traversal

The binary tree is a type of tree in which each and every node has atleast two children except the leaf nodes. We have various operations in the binary tree,...

4 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Linked List Data Structure

Linked list in DS: The linked list is a non-primitive and linear data structure. It is a list of a particular type of data element that is connected to each...

3 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

3 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Segregate Even and Odd nodes in a Linked List

Segregate even and odd nodes in a Linked List In this problem, we have given a linked list with integer numbers. We need to modify the given linked list in such...

4 minutes read.

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children...

4 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Inorder Successor in Binary Trees

The next node in the Inorder traversal of a binary tree is known as Inorder successor of that particular node. In a Binary Search Tree, the definition of Inorder successor can...

9 minutes read.

Insertion Sort vs Selection Sort

In this article, we will discuss insertion sort, Selection sort and the basic differences between these two sorting techniques in detail: What is Insertion Sort? Insertion Sort – The insertion sort is...

5 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Introduction to 1D-Arrays

One Dimensional Array Technical Definitions The simplest version of an Array is a One-Dimensional Array, in which the items are stored linearly and may be accessed individually by supplying the index value...

6 minutes read.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.