×

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

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

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

Write Main Difference Between Tree and Graph in Data Structures

Graph: The graph has two sets, which are considered V and E. These vertices are also called nodes, and edges are referred to as arcs connecting any two nodes in a...

4 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Arrange consonants and vowels nodes in a linked list

Arrange consonants and vowels nodes in a linked list In this problem, we have given a singly linked list. Here we will arrange the consonants and vowels nodes of the list...

2 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

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.

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.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

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

Delete nodes from the linked list which have a greater value on the right side

Delete nodes from the linked list which have a greater value on the right side In this problem, we have given a singly linked list, and we need to remove all...

3 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

Winner tree in Data Structures

Tree Data structure A tree is a hierarchical and non-linear data structure with nodes. Each node in the Tree contains a message value and stores the name passed to another ("child")...

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

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.