×

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

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.

Sort the linked list of 0s, 1s and 2s

Sort the linked list of 0s, 1s and 2s In this, we are given a linked list of 0s, 1s, and 2s, and we need to sort it. Examples: Input: 1  ->  1 ...

2 minutes read.

Reverse a Linked List in groups of given size

Reverse a Linked List in groups of given size This article will explain how to reverse a linked list in groups of given size. Here we have given a linked list...

2 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Difference between Structured and Object-Oriented Analysis

Analysis means observing and collecting relevant information about the structure of something or the basic details of a system's requirements. Structured and Object Oriented Analysis are both widely used in...

2 minutes read.

Introduction to 2D-Arrays

Two Dimensional Array Technical Definitions An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table...

3 minutes read.

Create a binary search tree

Implementation In this section of the article, we will see the usage and mechanism of how we will create a given binary tree. Let's observe these in more depth and then...

7 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

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

Sorting Algorithms in Data Structures

A sorting algorithm is used to organize the elements of an array or list. Sorting an array, for example. Unsorted array 572941 Sorted array 124579 We're sorting the array in ascending order right now. This procedure...

4 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

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

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Circular Queue

Circular Queue Circular Queue is special type queue, which follows First in First Out (FIFO) rule and as well as instead of ending queue at the last position, it starts again...

4 minutes read.

Operations of B Tree in C++ Language

B tree tends to be a self-aligning and balancing tree that helps us organise our data and document safely. We know that every data or information in the B tree...

9 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.