0% found this document useful (0 votes)
4 views9 pages

Task 4

The document contains a lab assignment by Sohil Sheth for the CSE2003 course, detailing programs for converting heaps to min and max heaps, performing quick sort, and insertion sort on given arrays. Each section includes algorithms, code implementations in C++, and expected outputs. The assignment demonstrates various sorting techniques and heap operations.

Uploaded by

sohilsheth21
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)
4 views9 pages

Task 4

The document contains a lab assignment by Sohil Sheth for the CSE2003 course, detailing programs for converting heaps to min and max heaps, performing quick sort, and insertion sort on given arrays. Each section includes algorithms, code implementations in C++, and expected outputs. The assignment demonstrates various sorting techniques and heap operations.

Uploaded by

sohilsheth21
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/ 9

Name: Sohil Sheth

Reg. No: 20BML0010

LAB ASSIGNMENT 4

CSE2003-Data Structures and Algorithms

Winter Semester 2022-23

NAME: SOHIL SHETH


REG. NO.: 20BML0010

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

int right = 2 * i + 2; // right child index

// If left child is smaller than root


if (left < n && arr[left] < arr[smallest])
smallest = left;

// If right child is smaller than smallest so far


if (right < n && arr[right] < arr[smallest])
smallest = right;

// If smallest is not root


if (smallest != i) {
swap(arr[i], arr[smallest]);

// Recursively min heapify the affected sub-tree


minHeapify(arr, n, smallest);
}
}
void buildMinHeap(int arr[], int n) {
// Index of last non-leaf node
int startIdx = (n / 2) - 1;

// Perform reverse level order traversal


for (int i = startIdx; i >= 0; i--) {
minHeapify(arr, n, i);
}
}
// Function to delete the minimum element from the min heap
int deleteMin(int arr[], int& n) {
int minElement = arr[0];
arr[0] = arr[n-1];
n--;
minHeapify(arr, n, 0);
return minElement;
}
int main() {
int arr[] = {14, 12, 3, 5, 9, 7, 5, 4, 12, 13, 22};
cout<<" Before sorting: 14 12 3 5 9 7 5 4 12 13 22"<<endl;
int n = sizeof(arr) / sizeof(arr[0]);
int m = n;
// Convert the given heap to min heap
buildMinHeap(arr, n);

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);
}

// Print the sorted array


cout << "Sorted Array: ";
for (int i = 0; i < sortedArray.size(); i++) {
// cout<<i<<endl;
cout << sortedArray[i] << " ";
}
cout << endl;

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

 De ine a function heap_sort(arr) that takes an array as input.


 Call the build_max_heap function on the array to convert it into a max heap.
 Iterate through the heap in reverse order, exchanging the root with the inal
piece, and executing max heapify on the remaining heap to restore the maximum
heap property.
 Add the root element (the maximum element) to the end of a sorted list.

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 left child is larger than root


if (left < n && arr[left] > arr[largest])
largest = left;
// If right child is larger than largest so far
if (right < n && arr[right] > arr[largest])
largest = right;

if (largest != i) {
swap(arr[i], arr[largest]);

// Recursively max heapify the affected sub-tree


maxHeapify(arr, n, largest);
}
}

// Function to convert an array to max heap


void buildMaxHeap(int arr[], int n) {
// Index of last non-leaf node
int startIdx = (n / 2) - 1;

// Perform reverse level order traversal


for (int i = startIdx; i >= 0; i--) {
maxHeapify(arr, n, i);
}
}
// Function to delete the maximum element from the max heap
int deleteMax(int arr[], int& n) {
int maxElement = arr[0];
arr[0] = arr[n-1];
n--;
maxHeapify(arr, n, 0);
Name: Sohil Sheth
Reg. No: 20BML0010

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;

// Convert the given heap to max heap


buildMaxHeap(arr, 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);
}

// Print the sorted array


cout << "Sorted Array using the MaxHeap: ";
for (int i = 0; i < sortedArray.size(); i++) {
cout << sortedArray[i] << " ";
}
cout << endl;

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;

void swap(int* a, int* b)


{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i
= (low
- 1); // Index of smaller element and indicates

for (int j = low; j <= high - 1; j++) {


if (arr[j] < pivot) {
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high) {
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size)


Name: Sohil Sheth
Reg. No: 20BML0010

{
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;

// Function to sort an array using


// insertion sort
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}

// 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:

You might also like