Lab Task 34 Id 466
Lab Task 34 Id 466
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 :
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
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;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
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);
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (smallest != i) {
swap(arr[i], arr[smallest]);
heapify(arr, n, smallest);
}
}
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);
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
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);
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>
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;
}