2. Md. Sultanul Islam Ovi
Lecturer
Depart. of CSE, GUB
Presented to :
Presented by :
Arafath Islam Sezan
ID-213902035
3. 01 02
Heap Sort Type of Heap Sort
04 05 06
Problem Scenarios
Table of contents
03
Time Complexity A Competitive
Problem
Input and Output
07
CONCLUSION
4. Heap Sort
Heap sort is a comparison-based sorting
technique based on Binary Heap data
structure.
There is Two type of Heap Sort
● Max Heap Sort
● Min Heap Sort
5. Problem Scenarios Of Heap Sort
● Sorting a list of products based on their prices.
● Sorting a list of cities based on their populations.
● Sorting a list of books based on their publication dates.
6. Array Implementation :-
For any node, the
following formulas apply:
The index
of its parent =i *2
Index of left
child = 2 * i+1
Index of
right child = 2*i+2
Algorithm of Heap Sort
8. A Competitive Problem—
Sorting a list of products based on their prices.
Here is the code -
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Product {
private String name;
private double price;
9. public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
public class HeapSortProductsByPrice {
public static void main(String[] args) {
List<Product> products = getProductsFromUser();
heapSort(products);
System.out.println("Products sorted by price (ascending order):");
for (Product product : products) {
System.out.println(product.getName() + " - Price: $" + product.getPrice());
}
}
10. private static List<Product> getProductsFromUser() {
List<Product> products = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of products: ");
int numProducts = scanner.nextInt();
scanner.nextLine();
for (int i = 1; i <= numProducts; i++) {
System.out.println("Product " + i + ":");
System.out.print("Enter the name: ");
String name = scanner.nextLine();
System.out.print("Enter the price: ");
double price = scanner.nextDouble();
scanner.nextLine();
products.add(new Product(name, price));
}
scanner.close();
return products;
}
11. private static void heapSort(List<Product> products) {
int n = products.size();
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(products, n, i);
}
for (int i = n - 1; i > 0; i--) {
swap(products, 0, i);
heapify(products, i, 0);
}
}
private static void heapify(List<Product> products, int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // Left child
int right = 2 * i + 2; // Right child
if (left < n && products.get(left).getPrice() > products.get(largest).getPrice()) {
largest = left;
}
if (right < n && products.get(right).getPrice() > products.get(largest).getPrice()) {
largest = right;
}
12. if (largest != i) {
swap(products, i, largest);
heapify(products, n, largest);
}
}
private static void swap(List<Product> products, int i, int j) {
Product temp = products.get(i);
products.set(i, products.get(j));
products.set(j, temp);
}
}