DSA-Heap Sort

Heap Sort:

Before starting the heap sort we need to know the Tree Binary Data Structure very well.

Binary Tree Data Structure:

Binary Tree DS has one parent node and at least two child node (left and right).

1480-binary-tree-coloring-game.png

We can classify Binary tree into three types,

  1. Full Binary Tree
  2. Complete Binary Tree
  3. Almost Binary Tree.

let see one by one now,

Rules applicable for Binary Tree:

  • We can move towards next level only when filling the current node value.
  • We can fill the right node value only when filling the left node value

Full Binary Tree:

Every Node should have 2 child node apart from leaf nodes.

image.png

Complete Binary Tree:

There is no restriction of child node(In FBT we should have 2 child node at leaf nodes). We may have one or two nodes at leaf nodes.

image.png

Almost Complete Binary Tree:

Always leaf node should have one child.

Properties of Binary Tree

  • number of levels = 0 to n
  • number of nodes = 2^k - 1
  • number of levels -> k = log (n + 1)
  • number of leaf nodes (n/2) - Upper bound
  • number of non - leaf nodes (n/2) - Lower bound

image.png

Now we will see implementation of Heap sort,

Heap Sort Implementation:

Heap sort uses the CBT and array data structure ( To save the space)

There are two type of heap sort one is min heap and another one is max heap,

image.png

Min Heap:

Parent node value should always lesser than the child node values.

Max heap:

Parent node value should always greater than the child node values.

Application:

It used to find max or min values.

0
Subscribe to my newsletter

Read articles from Sivaraman Arumugam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sivaraman Arumugam
Sivaraman Arumugam

I am a data engineer who is responsible for designing, building, maintaining, and testing the infrastructure and systems that are used to store, process, and analyze data. I work closely with data scientists and analysts to ensure that the data pipelines and systems are able to support the data needs of an organization. I have a strong background in computer science and software engineering, and skilled in programming languages such as Python, Java, and SQL also familiar with database systems and big data technologies like Hadoop, Spark, and NoSQL databases. Some of my key responsibilities as a data engineer: Designing and building data pipelines to extract, transform, and load data from various sources Setting up and maintaining data storage and processing systems, including data warehouses and data lakes Collaborating with data scientists and analysts to understand their data needs and ensure that the data infrastructure can support their requirements Performing data quality checks and troubleshooting any issues that arise Implementing security and privacy measures to protect sensitive data