×

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

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

Recursion in Fibonacci

Fibonacci heap is considered to be a particular execution of the heap data structure that ultimately helps in making use of not just any number but the Fibonacci numbers. It...

3 minutes read.

Binary Search

Binary Search: When there is a large data structure, the linear search takes a lot of time to search the element. The binary search was developed to overcome the lack...

7 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

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.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

What is a Sparse Matrix in Data Structure?

Definition A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix...

5 minutes read.

What is a Tree in Terms of a Graph?

To know the explanation of trees in terms of graphs, we need first to know what trees and graphs are. So let us first learn about trees and graphs. Trees and...

6 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Adding one to the number represented an array of digits

You have given one array, which consists of values which represent the different digits of a number. You have to add 1 to this number and store the result in...

3 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

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

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Given a Binary Tree, Print the Pre-order Traversal in Recursive

Implementation #include <stdio.h> #include <stdlib.h>   /* Creating a binary tree node that consists of some data along with the pointer to the left and right child.  */ struct __nod {     int record;     struct...

4 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.