practical 5
practical 5
PRACTICAL 5
AIM: Implementation and comparison of Insertion sort and Heap sort algorithm.
THEORY:
Sorting is a fundamental operation in computer science used to arrange elements in a specific
order, typically ascending or descending. In this practical, we implement and compare two
sorting algorithms: Insertion Sort and Heap Sort. While Insertion Sort is simple and efficient for
small datasets, Heap Sort is more suitable for larger datasets due to its efficient time complexity.
Insertion Sort
Definition
Insertion Sort is a simple comparison-based sorting algorithm that builds the final sorted array
one element at a time by inserting elements into their correct position.
Working Mechanism
1. It starts with the second element, assuming the first element is already sorted.
2. The current element is compared with the elements in the sorted part (left side).
3. If the current element is smaller, it is shifted left until it reaches its correct position.
4. This process continues until all elements are sorted.
Properties of Insertion Sort
1. In-place Sorting: It does not require extra space for sorting, making it memory efficient
(O(1) space complexity).
2. Stable Sort: It preserves the order of equal elements, making it suitable for sorting records
with multiple fields.
3. Adaptive Nature: It performs well on nearly sorted data, reducing its time complexity to
O(N) in the best case.
4. Online Algorithm: It can sort data as it receives it, making it useful for real-time
applications.
Real-World Applications of Insertion Sort
• Sorting small datasets where efficiency is not a primary concern.
• Hand sorting techniques, like arranging playing cards.
• When data arrives sequentially and needs to be sorted in real-time (e.g., live leaderboard
updates).
Page | 34
CO23329
DOP – 30/01/25 DOS – 05/02/25
Heap Sort
Definition
Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure to
organize and sort elements. It follows the Heap Property, where the parent node is always
greater (max heap) or smaller (min heap) than its children.
Working Mechanism
1. Convert the array into a max heap (largest element at the root).
2. Swap the root (largest element) with the last element of the heap.
3. Reduce the heap size and apply heapify to maintain the heap property.
4. Repeat the process until the array is sorted.
Properties of Heap Sort
1. Comparison-Based Sorting: It compares elements to maintain the heap property.
2. Efficient Time Complexity: Unlike Insertion Sort, it has a consistent O(N log N)
complexity in all cases.
3. Not a Stable Sort: Heap Sort does not preserve the order of equal elements after sorting.
4. In-place Sorting: It sorts data within the same array without needing extra memory.
5. Non-Recursive Sorting: Although heapify is often implemented recursively, it can be
written iteratively to avoid extra function call overhead.
Real-World Applications of Heap Sort
• Priority Queues: Used in scheduling processes in operating systems.
• Graph Algorithms: Used in Dijkstra’s Shortest Path Algorithm and Prim’s Minimum
Spanning Tree.
• Data Streaming: Sorting large sets of real-time data efficiently.
• Heap-Based Data Structures: Used in implementations of heaps, priority queues, and job
scheduling.
o Shift all elements greater than key one position to the right.
o Insert key into its correct position.
4. Repeat the above steps for all elements until the entire array is sorted.
PSEUDOCODE OF INSERTION SORT :
InsertionSort(arr, n):
for i = 1 to n-1:
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j=j-1
arr[j + 1] = key
FLOW CHART :
Page | 36
CO23329
DOP – 30/01/25 DOS – 05/02/25
Heapify(arr, n, i):
largest = i
left = 2*i + 1
right = 2*i + 2
Page | 37
CO23329
DOP – 30/01/25 DOS – 05/02/25
if largest != i:
swap(arr[i], arr[largest])
heapify(arr, n, largest)
FLOW CHART:
CODE IMPLEMENTATION:
#include <iostream>
using namespace std;
Page | 38
CO23329
DOP – 30/01/25 DOS – 05/02/25
if (max != i) {
swap(arr[i], arr[max]);
heapify(arr, n, max);
}
Page | 39
CO23329
DOP – 30/01/25 DOS – 05/02/25
int main() {
int n, choice;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
Page | 40
CO23329
DOP – 30/01/25 DOS – 05/02/25
switch (choice) {
case 1:
insertionSort(arr, n);
cout << "Sorted array using Insertion Sort: ";
break;
case 2:
heapSort(arr, n);
cout << "Sorted array using Heap Sort: ";
break;
default:
cout << "Invalid choice!";
return 1;
}
printArray(arr, n);
return 0;
}
Page | 41
CO23329
DOP – 30/01/25 DOS – 05/02/25
OUTPUT:
Page | 42
CO23329