0% found this document useful (0 votes)
25 views18 pages

ClassNotes 5 - DSA (Heap)

The document provides an overview of various sorting algorithms, their types, time and space complexities, stability, and use cases. It also explains binary trees, including full and complete binary trees, max and min heaps, and the processes of inserting and deleting nodes in heaps. Additionally, it covers heapification and the concept of priority queues.

Uploaded by

hiisumit2u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views18 pages

ClassNotes 5 - DSA (Heap)

The document provides an overview of various sorting algorithms, their types, time and space complexities, stability, and use cases. It also explains binary trees, including full and complete binary trees, max and min heaps, and the processes of inserting and deleting nodes in heaps. Additionally, it covers heapification and the concept of priority queues.

Uploaded by

hiisumit2u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Type of Sorting:
Sorting Algorithm Type Time Complexity Space Complexity Stable Use Case

Bubble Sort Comparison-Based Best: O(n), Average/Worst: O(n²) O(1) Yes Simple, educational; small datasets

Selection Sort Comparison-Based Best/Average/Worst: O(n²) O(1) No Small datasets, minimizing swaps

Insertion Sort Comparison-Based Best: O(n), Average/Worst: O(n²) O(1) Yes Small or nearly sorted datasets; hybrid algorithms

Merge Sort Comparison-Based Best/Average/Worst: O(n log n) O(n) Yes Linked lists, stable sorting, external sorting

Quick Sort Comparison-Based Best/Average: O(n log n), Worst: O(n²) O(log n) No In-memory sorting, general-purpose (e.g., std::sort)

Heap Sort Comparison-Based Best/Average/Worst: O(n log n) O(1) No Guaranteed O(n log n) with minimal space

Counting Sort Non-Comparison-Based O(n + k), k = range of values O(k) Yes Small integer ranges

Radix Sort Non-Comparison-Based O(d(n + k)), d = digits, k = base O(n + k) Yes Integers, fixed-length strings

Bucket Sort Non-Comparison-Based Best/Average: O(n + k), Worst: O(n²) O(n + k) Depends Uniformly distributed data (e.g., floats)

Timsort Hybrid Best: O(n), Average/Worst: O(n log n) O(n) Yes General-purpose (Python, Java default)

Introsort Hybrid Best/Average/Worst: O(n log n) O(log n) No General-purpose (C++ STL std::sort)

Best: O(n log n), Worst: O(n²)


Shell Sort Comparison-Based O(1) No Moderate-sized datasets
(depends on gap)

Cocktail Sort Comparison-Based Best: O(n), Average/Worst: O(n²) O(1) Yes Slightly better than bubble sort; rarely used

Comb Sort Comparison-Based Best/Average: O(n log n), Worst: O(n²) O(1) No Improved bubble sort; rarely used

Notes:
 Time Complexity: "Best" refers to the best-case scenario (e.g., nearly sorted input for adaptive algorithms), "Average" is the expected case, and "Worst" is the worst-case scenario.
 Space Complexity: Indicates additional memory used (excluding input array). O(1) means in-place.
 Stable: Yes if the algorithm preserves the relative order of equal elements.
 Use Case: Practical scenarios where the algorithm is most effective.

1. Binary Table Basic:

Index 1 2 3 4 5 6 7
Element A B C D E F G

 If a Node is at index – ⅈ

 If Left child is at – 2∗ⅈ (if index is starting with 0; A[2i +1])

 If ¿ child is at – 2∗ⅈ +1 (if index is starting with 0; A[2i +2])


 Its parent is at - ⌊ ⌋ (floor value)
2

Examples
Floor ⌊x Ceiling ⌈x
⌋ ⌉
x Fractional part {x}

2 2 2 0

2.000
2 3 0.0001
1

2.4 2 3 0.4

2.9 2 3 0.9

2.999 2 3 0.999

−2.7 −3 −2 0.3

−2 −2 −2 0

2. Binary Table Basic:


If you don’t want to fill the table using formula. Then fill the binary table level by level and start with left:

Index 1 2 3 4 5
Element A B C D E

Index 1 2 3 4 5 6 7
Element A B C F G
3. Full vs Complete Binary Tree:

Feature Full Binary Tree Complete Binary Tree

Child Nodes Every node has 0 or 2 children Last level filled left-to-right

Structure No restrictions on level fill Must fill levels left-first

Example A node can have 0 or 2 kids No "missing" nodes before last

Full Binary Tree Complete Binary Tree Incomplete Binary Tree

4. Max vs Min Heap:


A. Max-Heap: Parent is greater than or equal to its children..

Index 1 2 3 4 5 6 7
Element 50 30 20 15 10 8 16
B. Min-Heap: Parent is less than or equal to its children.

Index 1 2 3 4 5 6 7
Element 10 30 20 35 40 32 25

Feature Max Heap Min Heap

Ordering Parent ≥ Children Parent ≤ Children

Root Maximum element Minimum element

Use Priority queues (highest first) Priority queues (lowest first)


Case

Example Used in HeapSort (descending) Used in Dijkstra’s algorithm

5.Insert in Heap

Step 1: Basic Heap

Index 1 2 3 4 5 6 7
Element 50 30 20 15 10 8 16

Step 2: Add element in Heap

Index 1 2 3 4 5 6 7 8
Element 50 30 20 15 10 8 16 60
Step 3: Do Heapification:

Index 1 2 3 4 5 6 7 8
Element 50 30 20 60 10 8 16 15

Index 1 2 3 4 5 6 7 8
Element 50 60 20 30 10 8 16 15

Index 1 2 3 4 5 6 7 8
Element 60 50 20 30 10 8 16 15

Time Complexity:

 Hight of Binary Tree is: O ( log n )


 How many swaps to insert a node: O ( log n )
 Time complexity to insert an element/node in heap is: O ( 1 ) ¿ O ( log n )

Please note:

 Add the new element as leaf Node (following complete binary tree rule)
 Then compare by ancestors towards upwards
6.Delete from Heap

Rules:

 Nodes from Root Node can be deleted only


 Once Root node deleted. Element from last node will replace the root node
 Now, the root node will compare with its child node and do heapification of Heap

Step 1: Delete the ‘Root’ element

Index 1 2 3 4 5 6 7
Element 50 30 20 15 10 8 16

Step 2: Element from last node will replace the root node

Index 1 2 3 4 5 6
Element 16 30 20 15 10 8

Index 1 2 3 4 5 6
Element 30 16 20 15 10 8

Time Complexity:

 Time complexity to deletion an element/node in heap is: O ( log n )

Note:

 The deleted element can be stored at the back of the array (empty space)

Index 1 2 3 4 5 6 7
Element 30 16 20 15 10 8 50
7.Heap Sort
1. Create a heap
2. Delete the heap

Index 1 2 3 4 5
Element 10 20 15 30 40
1. Create a Heap
Step 1: Add root node (10)

Index 1
Element 10

Step 2: Add second node (20)

Index 1 2
Element 10 20

Do heapification 1:

Index 1 2
Element 20 10

Step 3: Add third node (15)

Index 1 2 3

Element 20 10 15

Step 4: Add forth node (30)

Index 1 2 3 4

Element 20 10 15 30
Do heapification 1:

Index 1 2 3 4

Element 20 30 15 10

Do heapification 2:

Index 1 2 3 4

Element 30 20 15 10

Step 4: Add fifth node (40)

Index 1 2 3 4 5

Element 30 20 15 10 40

Do heapification 1:

Index 1 2 3 4 5

Element 30 40 15 10 20
Index 1 2 3 4 5

Element 40 30 15 10 20

2. Delete the Heap

Step 1: Delete Root Node 40

Index 1 2 3 4 5
Element 40 30 15 10 20
Do Heapification:

Index 1 2 3 4
Element 20 30 15 10

Do Heapification and deleted root node will be placed back of the array:

Index 1 2 3 4
Element 30 20 15 10 50

Step 2: Delete Root Node 30

Index 1 2 3 4
Element 30 20 15 10 50
Do Heapification:

Index 1 2 3
Element 10 20 15 50

Index 1 2 3
Element 20 15 10 50

Do Heapification and deleted root node will be placed back of the array:

Index 1 2 3
Element 20 15 10 30 50

Step 3: Delete Root Node 20

Index 1 2 3
Element 20 15 10 30 50

Do Heapification:

Index 1 2
Element 10 15 30 50

Index 1 2
Element 15 10 30 50

deleted root node will be placed back of the array:


Index 1 2
Element 15 10 20 30 50

Step 4: Delete Root Node 15

Index 1
Element 10 20 30 50

deleted root node will be placed back of the array:

Index 1
Element 10 15 20 30 50

Step 5: Last node (10)

Tree is sorted:

Index
Element 10 15 20 30 50

Time-Complexity to Insert Node:


 Total number of nodes: n
 Time-taken to insert a node: log n
 Total-time: n ⋅ log n

Time-Complexity to Delete Node:


 Total number of nodes: n
 Time-taken to delete a node: log n
 Total-time: n ⋅ log n

Total Time for Heap Sort: n ⋅ log n+ n⋅ log n=2 n ⋅log n


Time Complexity: O ¿

8.Heapification
✅ What is Heapification?
Heapification (also called heapify) is the process of converting a binary tree or array into a valid heap (either Min-Heap or Max-Heap), by making sure that every parent node follows
the heap property.

👇 Types of Heapification:
There are two main uses of heapify:

Type When It's Used What It Does


Bottom-up Heapify After building a heap from Starts from last non-leaf node and fixes heap
array
Top-down Heapify (also called Sift Down) After deleting the root Pushes the new root down to correct position

💡 Heap Property Recap:


 Max-Heap: Every parent node is greater than or equal to its children.
 Min-Heap: Every parent node is less than or equal to its children.

🔁 How Heapification Works (Top-Down Method):


Steps (Max-Heap):
1. Start at a node.
2. Compare it with left and right children.
3. If one of the children is greater than the parent, swap the largest child with the parent.
4. Repeat this process down the tree.

Example: Heapification - Max-Heap

1. Start at a node.
2. Compare it with left and right children.

Index 1 2 3 4 5 6 7
Element 10 20 15 12 40 25 18

Index 1 2 3 4 5 6 7
Element 10 20 25 12 40 15 18

Index 1 2 3 4 5 6 7
Element 10 20 15 12 40 25 18
Index 1 2 3 4 5 6 7
Element 10 40 15 12 20 15 18

Index 1 2 3 4 5 6 7
Element 40 10 25 12 20 15 18

Finally:

Index 1 2 3 4 5 6 7
Element 40 20 25 12 10 15 18

9.Heap Priority Queue

A Priority Queue is like a special kind of queue where each item has a priority.

🚫 Normal Queue:

o Works on FIFO → First In First Out


o Whoever comes first, goes first.

✅ Priority Queue:

o Works on Highest Priority First


o The item with the highest priority is removed first, no matter when it came.

💡 Real-Life Example:

Imagine a hospital emergency room:

o A patient with a heart attack will be treated before someone with a fever, even if the fever patient came first.
o Here, "heart attack" has higher priority.

🧠 How is Heap used in Priority Queue?

We use a Heap to efficiently manage the priority queue:

o In a Max-Heap, the element with the highest priority (value) is always at the top (root).
o In a Min-Heap, the element with the lowest value is at the top (used when smaller means higher priority).
🔁 Operations in Heap Priority Queue:

Operation Description Time

Insert(item) Add a new item to the heap and re-heapify O¿

Get Max / Min Look at the highest (or lowest) priority item O ¿)

Remove Max / Remove the top-priority item and re-heapify O¿


Min

1. Smaller Number – Higher Priority:


Array:

Index 1 2 3 4 5 6 7
Element 8 6 3 10 5 4 9

Priority Heap (Min-heap)

2. Large Number – Higher Priority:

Array:

Index 1 2 3 4 5 6 7
Element 8 6 3 10 5 4 9

Priority Heap (Max-heap)

 Time complexity to insert/delete an item from array is: O(n)


 Time complexity to insert/delete an item from Heap is: O(logn)
Max Heap, Min Heap, Build Heap and Heapification
🔺1. What is a Heap?
A Heap is a special Tree (usually a binary tree) that follows two rules:

1. Complete Binary Tree: All levels are full except possibly the last level, which is filled from left to right.

2. It must follow the Heap Property (explained below).

🟡 Difference between Max Heap, Min Heap, and Build Heap


Feature Max Heap Min Heap Build Heap

Definition Parent node is greater than children Parent node is smaller than Process to create a heap from an array
children

Root Node Highest value Lowest value Not a type of heap, but a method

Used for Sorting in Descending (Heap Sort) Sorting in Ascending (Heap Sort) Creating Max or Min Heap

Structure Explained below Explained below Explained below


Example

🔺 Max Heap Example


Array: [50, 30, 40, 10, 5, 20, 30]

This is how Max Heap looks like:

50

/ \

30 40

/ \ / \

10 5 20 30

➡️Rule: Every parent node is greater than its children.

🔻 Min Heap Example


Array: [5, 10, 20, 30, 50, 40, 60]

This is how Min Heap looks like:

/ \

10 20

/ \ / \

30 50 40 60

➡️Rule: Every parent node is smaller than its children.

What is Build Heap?


Build Heap is the process of converting any random array into a heap (either Min Heap or Max Heap).
🔧 Build Heap Example
Input Array: [5, 10, 30, 20, 35]

Step 1: Treat the array as a binary tree:

/ \

10 30

/ \

20 35

Step 2: Apply heapification from bottom to top.

✅ If we want Max Heap, we swap and rearrange so each parent is greater than children.

🔁 This is how the Build Heap process works — it turns an unsorted tree into a proper heap using heapify.

🧠 Summary
Term What it means

Max Heap Biggest element at the top

Min Heap Smallest element at the top

Build Heap Process to create a heap from an array

Let me know if you want step-by-step build heap code or animation example!

💡 What is Heapification?
Heapification is the process of fixing a binary tree (or part of it) so that it satisfies the heap property.

In simple words:

🎯 Heapify takes a node, checks its children, and makes sure the heap rule (Max or Min) is followed. If not, it swaps and keeps going down the tree.

Term Meaning

Heapify Fix the heap from a node down so that heap rules are
followed

Used in Build Heap, Insert, Delete

Max Heapify Parent must be bigger than children

Min Heapify Parent must be smaller than children

Time Complexity:
Operation Max Heap Time Min Heap Time Notes

Insert O (log n) O(log n) Heapify up to maintain order

Delete (Root) O (log n) O (log n) Heapify down to fix structure

Get Max / Min O (1) O (1) Root is always max/min

Build Heap O(n) O(n) Done using bottom-up heapify

Search any element O(n) O(n) Heap is not sorted like BST

Heap Sort O (n. log n) O (n. log n)

You might also like