Bangladesh University of Business & Technology
Course name:Algorithms Lab
Course code:CSE 232
Submitted by:Mehrab Hossain Alvy
Id:20234103039
Intake:52
Section:01
Submitted to:Sajid Ahmed Chowdhury
(Lecturer)
Dept. of CSE
Task 01:Sorting a Small List
You are given a small list of integers: [10, 3, 15, 7, 8, 23, 74, 18]. Use the Heap Sort algorithm to
sort the list in descending order.
• Input: [10, 3, 15, 7, 8, 23, 74, 18]
• Expected Output: [74, 23, 18, 15, 10, 8, 7, 3]
Code:
Output:
Task 02:Kth Largest Element in an Array
Given an unsorted list of integers: [5, 12, 7, 1, 3, 10, 9, 8], and an integer k = 2, find the
k-th largest element in the list using Heap Sort. Instead of sorting the entire list, adjust
the algorithm to find the k-th largest element efficiently.
Test Case:
• Input: [5, 12, 7, 1, 3, 10, 9, 8], k = 2
• Expected Output: 10
Code:
Output:
Task 03:Heap Sort with Custom Comparator
Given a list of strings: [”apple”, ”banana”, ”cherry”, ”date”, ”f ig”, ”grape”], sort the list
in descending order of string length using Heap Sort. If two strings have the same length,
maintain their original relative order (stable sorting).
Test Case:
• Input: [”apple”, ”banana”, ”cherry”, ”date”, ”f ig”, ”grape”]
• Expected Output: [”banana”, ”cherry”, ”apple”, ”grape”, ”date”, ”fig”]
Code:
Output: