0% found this document useful (0 votes)
8 views4 pages

Heap Sort

A heap data structure is a specialized binary tree where nodes are arranged based on their value. There are two main types: a min heap where the root node has the minimum value of all nodes, and a max heap where the root node has the maximum value. The heap sort algorithm works by transforming an array into a max heap, then repeatedly swapping the root node (largest element) with the last element and restoring the heap property, sorting the array from largest to smallest in O(n log n) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Heap Sort

A heap data structure is a specialized binary tree where nodes are arranged based on their value. There are two main types: a min heap where the root node has the minimum value of all nodes, and a max heap where the root node has the maximum value. The heap sort algorithm works by transforming an array into a max heap, then repeatedly swapping the root node (largest element) with the last element and restoring the heap property, sorting the array from largest to smallest in O(n log n) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Heap data structure is a specialized binary tree-based data structure.

Heap is a binary tree with


special characteristics. In a heap data structure, nodes are arranged based on their values. A heap
data structure some times also called as Binary Heap.

There are two types of heap data structures and they are as follows...

Max Heap

Min Heap
Min Heap
A Min Heap Binary Tree is a Binary Tree where the root node has the minimum key in the tree.
The above definition holds true for all sub-trees in the tree. This is called the Min Heap property.

Almost every node other than the last two layers must have two children. That is, this is almost a
complete binary tree, with the exception of the last 2 layers.

The below tree is an example of a min heap binary tree since the above two properties hold

Max Heap
A max heap is a complete binary tree in which the value of a node is greater than or equal to the values of
its children. Max Heap data structure is useful for sorting data using heap sort.

Difference between Max Heap and Min Heap


Min Heap Max Heap

In a Min-Heap the key present at the In a Max-Heap the key present at the
root node must be less than or equal to root node must be greater than or equal
among the keys present at all of its to among the keys present at all of its
1. children. children.

In a Min-Heap the minimum key element In a Max-Heap the maximum key


2. is present at the root. element is present at the root.

A Max-Heap uses the descending


A Min-Heap uses the ascending priority.
3. priority.

In the construction of a Min-Heap, the In the construction of a Max-Heap, the


4. smallest element has priority. largest element has priority.

In a Min-Heap, the smallest element is In a Max-Heap, the largest element is the


5. the first to be popped from the heap. first to be popped from the heap.

STEPS OF A HEAP SORT ALGORITHM

1. Transform the array into a binary tree by inserting each element as a node in a
breadth-first manner.
2. Convert the binary tree into a max heap, ensuring that all parent nodes are
greater than or equal to their child nodes.
3. Swap the root node — the largest element — with the last element in the heap.
4. Call the heapify() function to restore the max heap property.
5. Repeat steps 3 and 4 until the heap is sorted, and exclude the last element
from the heap on each iteration.
6. After each swap and heapify() call, ensure that the max heap property is
satisfied.
Complexity of the Heap Sort Algorithm

To sort an unsorted list with 'n' number of elements, following are the complexities...

Worst Case : O(n log n)


Best Case : O(n log n)
Average Case : O(n log n)

You might also like