SlideShare a Scribd company logo
1 of 15
Heap Sort
A presentation for CSE- 205
Md. Sultanul Islam Ovi
Lecturer
Depart. of CSE, GUB
Presented to :
Presented by :
Arafath Islam Sezan
ID-213902035
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
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
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.
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
Time Complexity
O ( n log n )
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;
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());
}
}
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;
}
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;
}
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);
}
}
Output
● Predictable time complexity.
● Suitable for scenarios.
Conclusion
Thank You

More Related Content

Similar to Heap sort - Arafath Islam Sezan.pptx

Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
rajeshjangid1865
 
Please the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdfPlease the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdf
admin463580
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docx
PeterlqELawrenceb
 
I really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdfI really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdf
pasqualealvarez467
 
Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256
Azhar Satti
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
aravlitraders2012
 
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdfC++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
info189835
 

Similar to Heap sort - Arafath Islam Sezan.pptx (20)

Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
Please the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdfPlease the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdf
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docx
 
How to optimize background processes - when Sylius meets Blackfire
How to optimize background processes - when Sylius meets BlackfireHow to optimize background processes - when Sylius meets Blackfire
How to optimize background processes - when Sylius meets Blackfire
 
I really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdfI really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdf
 
Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256
 
Qtp Training
Qtp Training Qtp Training
Qtp Training
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf
 
Lab 3
Lab 3Lab 3
Lab 3
 
Intake 37 linq2
Intake 37 linq2Intake 37 linq2
Intake 37 linq2
 
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdfC++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
 

Recently uploaded

ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
ashishpaul799
 

Recently uploaded (20)

2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon season
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptx
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 

Heap sort - Arafath Islam Sezan.pptx

  • 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); } }
  • 14. ● Predictable time complexity. ● Suitable for scenarios. Conclusion