Task 4
Task 4
LAB ASSIGNMENT 4
1. Write a program to convert given heap to MIN heap and perform the deletion
operation on MIN heap tree, print the elements in the sorted array:
14,12,3,5,9,7,5,4,12,13,22
Algorithm:
De ine a function min_heapify(arr, n, i) that takes an array, the size of the heap n,
and the index of the root i as input
Find the left and right children of the root at index i using the formula: left = 2i +
1, right = 2i + 2
Find the smallest element among the root, left child, and right child, then assign
its index to the smallest variable.
If the smallest element is not the root, the root should be replaced with the
smallest element.
Call the min heapify function recursively on the affected subtree, using the
smallest element as the root.
De ine the build min heap(arr, n) function, which accepts an array and the heap
size as inputs.
Iterate through the array in reverse order, calling min heapify on each index to
construct a minimum heap.
De ine a function heap_sort(arr) that takes an array as input.
Call the build_min_heap function on the array to convert it into a min heap.
Iterate through the heap, swapping the root with the last element, and calling the
min_heapify function on the remaining heap to restore the min heap property.
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Function to convert the given heap to a min heap
void minHeapify(int arr[], int n, int i) {
int smallest = i; // Initialize smallest as root
int left = 2 * i + 1; // left child index
Name: Sohil Sheth
Reg. No: 20BML0010
vector<int> sortedArray;
// Delete the minimum element from the min heap and add it to the sorted array
for (int i = 0; i < m; i++) {
int minElement = deleteMin(arr, n);
// cout<<minElement<<endl;
Name: Sohil Sheth
Reg. No: 20BML0010
sortedArray.push_back(minElement);
}
return 0;
}
Output:
2. Write a program to convert given heap to MAX heap and perform the deletion
operation on MAX heap tree, print the elements in the sorted array: 29,18,
30,25,57,48,42,53,34
Algorithm:
De ine a function max_heapify(arr, n, i) that takes an array, the size of the heap n,
and the index of the root i as input
Find the left and right children of the root at index i using the formula: left = 2i +
1, right = 2i + 2
Determine the largest element among the root, the left child, and the right child,
and save its index in the variable largest.
If the largest element is not the root, the root should be changed to the largest
element.
Call the max heapify function recursively on the impacted subtree, using the new
biggest node as the root.
De ine a function build max heap(arr, n) that accepts an array and the heap size
as arguments.
Iterate through the array in reverse order and call the max_heapify function on
each index to build a max heap.
Name: Sohil Sheth
Reg. No: 20BML0010
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void maxHeapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child index
int right = 2 * i + 2; // right child index
if (largest != i) {
swap(arr[i], arr[largest]);
return maxElement;
}
int main() {
int arr[] = {29, 18, 30, 25, 57, 48, 42, 53, 34};
cout<<"Before using MaxHeap: 29 18 30 25 57 48 42 53 34"<<endl;
int n = sizeof(arr) / sizeof(arr[0]);
int m = n;
vector<int> sortedArray;
// Delete the maximum element from the max heap and add it to the sorted array
for (int i = 0; i < m; i++) {
int maxElement = deleteMax(arr, n);
sortedArray.push_back(maxElement);
}
return 0;
}
Output:
Name: Sohil Sheth
Reg. No: 20BML0010
3. Write a program to perform the Quick sort for the given array. 2,7,3,9,1,6,8,4
Algorithm:
It uses divide and conquer algorithm.
Pick an element from an array, call it as pivot element.
Divide an unsorted array element into two arrays.
If the value less than pivot element come under first sub array, the remaining
elements with value greater than pivot come in second sub array.
Code:
#include <bits/stdc++.h>
using namespace std;
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver Code
int main()
{
int arr[] = {2,7,3,9,1,6,8,4};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array after quick sort implementation: \n";
printArray(arr, n);
return 0;
}
Output:
4. Write a program to perform the insertion sort for the given array.
4,3,2,10,12,1,5,6
Algorithm:
Start with the second element of the array, and compare it with the first element. If the
second element is smaller, swap it with the first element.
Move on to the third element and compare it with the second element. If the third element
is smaller, swap it with the second element. Then, compare the second element with the first
element and swap if necessary.
Repeat step 2 for all subsequent elements, comparing each element with the previous
elements and swapping if necessary.
Con nue un l the en re array is sorted in ascending order.
Pseudo code:
procedure insertionSort(A: list of sortable items)
n = length(A)
for i = 1 to n - 1 do
j = i
while j > 0 and A[j-1] > A[j] do
swap(A[j], A[j-1])
j = j - 1
Name: Sohil Sheth
Reg. No: 20BML0010
end while
end for
end procedure
Code:
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int arr[] = {4,3,2,10,12,1,5,6};
int N = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, N);
cout << "Sorted array after insertion sort implementation: \n";
printArray(arr, N);
return 0;
}
Name: Sohil Sheth
Reg. No: 20BML0010
Output: