0% found this document useful (0 votes)
14 views13 pages

Lab Task 34 Id 466

The document discusses implementations of heap sort, quick sort, and their variations. It explains heap sort and provides code to implement max heap and min heap sorting. It also explains quick sort and provides code for ascending and descending quick sort. Quick sort partitions an array around a pivot, and recursively sorts subarrays on each side. Heap sort uses a heap data structure to sort an array in ascending or descending order.

Uploaded by

Md Tariful Islam
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)
14 views13 pages

Lab Task 34 Id 466

The document discusses implementations of heap sort, quick sort, and their variations. It explains heap sort and provides code to implement max heap and min heap sorting. It also explains quick sort and provides code for ascending and descending quick sort. Quick sort partitions an array around a pivot, and recursively sorts subarrays on each side. Heap sort uses a heap data structure to sort an array in ascending or descending order.

Uploaded by

Md Tariful Islam
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/ 13

ASSIGNMENTS 03 & 04

Submitted To:
Submitted By:
Faria Binte Kader
Name : Md Faizer Islam
Lecturer
ID: 21225103466
Dept of C.S.C (BUBT)
Intake & Sec : 49 (10)
Task A
Build Max Heap :

void heapify(int arr[], int n, int i) {

int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

Max Heap : Where the value of the root node is greater than or
equal to either of its children.
Task B
Max Heap Implementation:
#include <iostream>
using namespace std;

void heapify(int arr[], int n, int i) {

int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

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

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--)


heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]);

heapify(arr, i, 0);
}
}

int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
heapSort(arr, n);

cout << "Sorted array is \n";


for (int i = 0; i < n; ++i)
cout << arr[i] << " ";

Max Heap : A max-heap is a complete binary tree in which the


value in each internal node is greater than or equal to the values
in the children of that node.
Task C
Min Heap Implementation:
#include <iostream>
using namespace std;

void heapify(int arr[], int n, int i) {

int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

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


smallest = left;

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


smallest = right;

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

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--)


heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]);

heapify(arr, i, 0);
}
}

int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
heapSort(arr, n);

cout << "Sorted array is \n";


for (int i = 0; i < n; ++i)
cout << arr[i] << " ";

Min heap : In a Min-Heap the key present at the root node must
be less than or equal among the keys present at all of its
children.
Task a

Quick sort Algorithm:

quickSort(array, leftmostIndex, rightmostIndex)


if (leftmostIndex < rightmostIndex)
pivotIndex <- partition(array,leftmostIndex,
rightmostIndex)
quickSort(array, leftmostIndex, pivotIndex - 1)
quickSort(array, pivotIndex, rightmostIndex)

partition(array, leftmostIndex, rightmostIndex)


set rightmostIndex as pivotIndex
storeIndex <- leftmostIndex - 1
for i <- leftmostIndex + 1 to rightmostIndex
if element[i] < pivotElement
swap element[i] and element[storeIndex]
storeIndex++
swap pivotElement and element[storeIndex+1]
return storeIndex + 1
Task b
Quick Sort Ascending order Implementation:
#include <bits/stdc++.h>
#include <vector>

using namespace std;

void swap(int& a, int& b) {


int temp = a;
a = b;
b = temp;
}

int partition(vector<int>& arr, int low, int high) {


int pivot = arr[high];
int i = low - 1;

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


if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}

swap(arr[i + 1], arr[high]);


return i + 1;
}

void quickSort(vector<int>& arr, int low, int high) {


if (low < high) {
int pivotIndex = partition(arr, low, high);

quickSort(arr, low, pivotIndex - 1);


quickSort(arr, pivotIndex + 1, high);
}
}

int main() {
int N;
cout << "Enter the size of the array: ";
cin >> N;

vector<int> arr(N);
cout << "Enter the unsorted integer numbers:\n";
for (int i = 0; i < N; i++) {
cin >> arr[i];
}

quickSort(arr, 0, N - 1);

cout << "Sorted array: ";


for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

Explanation :
Quick Sort is a popular, efficient, and widely used sorting
algorithm based on the divide-and-conquer technique. It was
developed by Tony Hoare in 1960. The basic idea of the Quick
Sort algorithm is to select a pivot element from the array and
partition the array into two sub-arrays: one containing elements
smaller than the pivot and the other containing elements
greater than the pivot. The pivot element is then placed in its
correct sorted position. This process is recursively applied to the
sub-arrays until the entire array is sorted.
Task c
Quick Sort Implementation dscending:

#include <iostream>
#include <vector>

using namespace std;

void swap(int& a, int& b) {


int temp = a;
a = b;
b = temp;
}

int partition(vector<int>& arr, int low, int high) {


int pivot = arr[low]; // Selecting the pivot as the
first element
int i = high + 1;

for (int j = high; j > low; j--) {


if (arr[j] >= pivot) { // For descending order,
change the comparison to '>='
i--;
swap(arr[i], arr[j]);
}
}
swap(arr[i - 1], arr[low]);
return i - 1;
}

void quickSortDescending(vector<int>& arr, int low,


int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);

quickSortDescending(arr, low, pivotIndex - 1);


quickSortDescending(arr, pivotIndex + 1,
high);
}
}

int main() {
int N;
cout << "Enter the size of the array: ";
cin >> N;

vector<int> arr(N);
cout << "Enter the unsorted integer numbers:\n";
for (int i = 0; i < N; i++) {
cin >> arr[i];
}

quickSortDescending(arr, 0, N - 1);
cout << "Sorted array in descending order: ";
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

Explanation : Quick Sort is a popular, efficient, and widely


used sorting algorithm based on the divide-and-conquer
technique. It was developed by Tony Hoare in 1960. The basic
idea of the Quick Sort algorithm is to select a pivot element from
the array and partition the array into two sub-arrays: one
containing elements smaller than the pivot and the other
containing elements greater than the pivot. The pivot element is
then placed in its correct sorted position. This process is
recursively applied to the sub-arrays until the entire array is
sorted

You might also like