SlideShare a Scribd company logo
1 of 2
Download to read offline
In Java code, implement the HeapSort algorithm. Your function must take an ArrayList as input
and output a sorted ArrayList. Allow the program to accept user input that becomes added to the
ArrayList, if no elements are added by the user, handle the exception.
Solution
Hi, Please find my program.
Please let me know in case of any issue.
import java.util.ArrayList;
import java.util.Scanner;
//Java program for implementation of Heap Sort
public class HeapSort
{
public void sort(ArrayList list)
{
int n = list.size();
// Build heap (relistangelistay)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(list, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = list.get(0);
list.set(0, list.get(i));
list.set(i, temp);
// call max heapify on the reduced heap
heapify(list, i, 0);
}
}
// To heapify a subtree rooted with node i which is
// an index in list[]. n is size of heap
void heapify(ArrayList list, int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && list.get(l) > list.get(largest))
largest = l;
// If right child is larger than largest so far
if (r < n && list.get(r) > list.get(largest))
largest = r;
// If largest is not root
if (largest != i)
{
int swap = list.get(i);
list.set(i, list.get(largest));
list.set(largest, swap);
// Recursively heapify the affected sub-tree
heapify(list, n, largest);
}
}
/* A utility function to print list of size n */
static void printlist(ArrayList list)
{
int n = list.size();
for (int i=0; i list = new ArrayList();
System.out.print("How many elements you want to sort: ");
int n = sc.nextInt();
System.out.println("Enter "+n+" integers: ");
for(int i=0; i

More Related Content

Similar to In Java code, implement the HeapSort algorithm. Your function must t.pdf

In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
EvanpZjSandersony
 
Create a JAVA Program that traverses through an array recursively- rat.docx
Create a JAVA Program that traverses through an array recursively- rat.docxCreate a JAVA Program that traverses through an array recursively- rat.docx
Create a JAVA Program that traverses through an array recursively- rat.docx
mrichard5
 
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
ssuserc77a341
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
MARRY7
 
Javathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfJavathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdf
eyelineoptics
 
Write a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfWrite a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdf
ebrahimbadushata00
 
In inline heapsort we use only single array throughout the program t.pdf
In inline heapsort we use only single array throughout the program t.pdfIn inline heapsort we use only single array throughout the program t.pdf
In inline heapsort we use only single array throughout the program t.pdf
anuragperipheral
 
Implement a function called getElements() that takes in two lists. T.pdf
Implement a function called getElements() that takes in two lists. T.pdfImplement a function called getElements() that takes in two lists. T.pdf
Implement a function called getElements() that takes in two lists. T.pdf
arracollection
 
I need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdfI need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdf
FORTUNE2505
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
lohithkart
 
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdfJAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
suresh640714
 

Similar to In Java code, implement the HeapSort algorithm. Your function must t.pdf (20)

In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
 
Create a JAVA Program that traverses through an array recursively- rat.docx
Create a JAVA Program that traverses through an array recursively- rat.docxCreate a JAVA Program that traverses through an array recursively- rat.docx
Create a JAVA Program that traverses through an array recursively- rat.docx
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
 
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
 
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
 
Javathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfJavathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdf
 
Write a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfWrite a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdf
 
In inline heapsort we use only single array throughout the program t.pdf
In inline heapsort we use only single array throughout the program t.pdfIn inline heapsort we use only single array throughout the program t.pdf
In inline heapsort we use only single array throughout the program t.pdf
 
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
 
When we first examined the array based and node based implementations.docx
 When we first examined the array based and node based implementations.docx When we first examined the array based and node based implementations.docx
When we first examined the array based and node based implementations.docx
 
Array list
Array listArray list
Array list
 
Implement a function called getElements() that takes in two lists. T.pdf
Implement a function called getElements() that takes in two lists. T.pdfImplement a function called getElements() that takes in two lists. T.pdf
Implement a function called getElements() that takes in two lists. T.pdf
 
I need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdfI need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdf
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
 
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdfJAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docx
 
week-19x
week-19xweek-19x
week-19x
 

More from arrowcomputers8700

Discuss the relationship between some of the common file classes in .pdf
Discuss the relationship between some of the common file classes in .pdfDiscuss the relationship between some of the common file classes in .pdf
Discuss the relationship between some of the common file classes in .pdf
arrowcomputers8700
 
describe the cell categories of nervous tissue and describe the orga.pdf
describe the cell categories of nervous tissue and describe the orga.pdfdescribe the cell categories of nervous tissue and describe the orga.pdf
describe the cell categories of nervous tissue and describe the orga.pdf
arrowcomputers8700
 
Describe what is meant by the Internet of Things. What industrie.pdf
Describe what is meant by the Internet of Things. What industrie.pdfDescribe what is meant by the Internet of Things. What industrie.pdf
Describe what is meant by the Internet of Things. What industrie.pdf
arrowcomputers8700
 
Create a class named Person. The person class contains first name, .pdf
Create a class named Person. The person class contains first name, .pdfCreate a class named Person. The person class contains first name, .pdf
Create a class named Person. The person class contains first name, .pdf
arrowcomputers8700
 
C++ Programming Language N-number queue rotations.Write methods v.pdf
C++ Programming Language N-number queue rotations.Write methods v.pdfC++ Programming Language N-number queue rotations.Write methods v.pdf
C++ Programming Language N-number queue rotations.Write methods v.pdf
arrowcomputers8700
 
2.Describe a technique that would allow a developmental biologist to.pdf
2.Describe a technique that would allow a developmental biologist to.pdf2.Describe a technique that would allow a developmental biologist to.pdf
2.Describe a technique that would allow a developmental biologist to.pdf
arrowcomputers8700
 
You are required to write an interactive C program that prompts the .pdf
You are required to write an interactive C program that prompts the .pdfYou are required to write an interactive C program that prompts the .pdf
You are required to write an interactive C program that prompts the .pdf
arrowcomputers8700
 
A living plant leaf is an open system that exchanges energy (sunligh.pdf
A living plant leaf is an open system that exchanges energy (sunligh.pdfA living plant leaf is an open system that exchanges energy (sunligh.pdf
A living plant leaf is an open system that exchanges energy (sunligh.pdf
arrowcomputers8700
 
What is dynamic semantics Can you please explain the many kinds of .pdf
What is dynamic semantics Can you please explain the many kinds of .pdfWhat is dynamic semantics Can you please explain the many kinds of .pdf
What is dynamic semantics Can you please explain the many kinds of .pdf
arrowcomputers8700
 

More from arrowcomputers8700 (20)

How are control charts madeSolutionThe control chart is a gra.pdf
How are control charts madeSolutionThe control chart is a gra.pdfHow are control charts madeSolutionThe control chart is a gra.pdf
How are control charts madeSolutionThe control chart is a gra.pdf
 
GENETICS Millie bought three packages of zinnia seeds for red, orang.pdf
GENETICS Millie bought three packages of zinnia seeds for red, orang.pdfGENETICS Millie bought three packages of zinnia seeds for red, orang.pdf
GENETICS Millie bought three packages of zinnia seeds for red, orang.pdf
 
Figure 5.2 shows a common biological phospholipid called phosphatidy.pdf
Figure 5.2 shows a common biological phospholipid called phosphatidy.pdfFigure 5.2 shows a common biological phospholipid called phosphatidy.pdf
Figure 5.2 shows a common biological phospholipid called phosphatidy.pdf
 
Discuss the relationship between some of the common file classes in .pdf
Discuss the relationship between some of the common file classes in .pdfDiscuss the relationship between some of the common file classes in .pdf
Discuss the relationship between some of the common file classes in .pdf
 
describe the cell categories of nervous tissue and describe the orga.pdf
describe the cell categories of nervous tissue and describe the orga.pdfdescribe the cell categories of nervous tissue and describe the orga.pdf
describe the cell categories of nervous tissue and describe the orga.pdf
 
Describe what is meant by the Internet of Things. What industrie.pdf
Describe what is meant by the Internet of Things. What industrie.pdfDescribe what is meant by the Internet of Things. What industrie.pdf
Describe what is meant by the Internet of Things. What industrie.pdf
 
Create a class named Person. The person class contains first name, .pdf
Create a class named Person. The person class contains first name, .pdfCreate a class named Person. The person class contains first name, .pdf
Create a class named Person. The person class contains first name, .pdf
 
C++ Programming Language N-number queue rotations.Write methods v.pdf
C++ Programming Language N-number queue rotations.Write methods v.pdfC++ Programming Language N-number queue rotations.Write methods v.pdf
C++ Programming Language N-number queue rotations.Write methods v.pdf
 
Assume a disease is dominant, indicated by shading. Based on the pedi.pdf
Assume a disease is dominant, indicated by shading. Based on the pedi.pdfAssume a disease is dominant, indicated by shading. Based on the pedi.pdf
Assume a disease is dominant, indicated by shading. Based on the pedi.pdf
 
Chorismate 3-glycero phosphate Relying on the above pathway for b.pdf
Chorismate 3-glycero phosphate Relying on the above pathway for b.pdfChorismate 3-glycero phosphate Relying on the above pathway for b.pdf
Chorismate 3-glycero phosphate Relying on the above pathway for b.pdf
 
A form of favoritism where you give preferential treatment to your f.pdf
A form of favoritism where you give preferential treatment to your f.pdfA form of favoritism where you give preferential treatment to your f.pdf
A form of favoritism where you give preferential treatment to your f.pdf
 
2.Describe a technique that would allow a developmental biologist to.pdf
2.Describe a technique that would allow a developmental biologist to.pdf2.Describe a technique that would allow a developmental biologist to.pdf
2.Describe a technique that would allow a developmental biologist to.pdf
 
4. Smircich and Morgan (1982), in an earlier reading, identified “le.pdf
4. Smircich and Morgan (1982), in an earlier reading, identified “le.pdf4. Smircich and Morgan (1982), in an earlier reading, identified “le.pdf
4. Smircich and Morgan (1982), in an earlier reading, identified “le.pdf
 
You are required to write an interactive C program that prompts the .pdf
You are required to write an interactive C program that prompts the .pdfYou are required to write an interactive C program that prompts the .pdf
You are required to write an interactive C program that prompts the .pdf
 
Which plant cell organelle contains its own DNA and ribosome A) Golg.pdf
Which plant cell organelle contains its own DNA and ribosome  A) Golg.pdfWhich plant cell organelle contains its own DNA and ribosome  A) Golg.pdf
Which plant cell organelle contains its own DNA and ribosome A) Golg.pdf
 
A living plant leaf is an open system that exchanges energy (sunligh.pdf
A living plant leaf is an open system that exchanges energy (sunligh.pdfA living plant leaf is an open system that exchanges energy (sunligh.pdf
A living plant leaf is an open system that exchanges energy (sunligh.pdf
 
Which of the following are typically used as input devices A. Print.pdf
Which of the following are typically used as input devices  A. Print.pdfWhich of the following are typically used as input devices  A. Print.pdf
Which of the following are typically used as input devices A. Print.pdf
 
What is dynamic semantics Can you please explain the many kinds of .pdf
What is dynamic semantics Can you please explain the many kinds of .pdfWhat is dynamic semantics Can you please explain the many kinds of .pdf
What is dynamic semantics Can you please explain the many kinds of .pdf
 
What happened to Mexicans in the west after the US annexed their lan.pdf
What happened to Mexicans in the west after the US annexed their lan.pdfWhat happened to Mexicans in the west after the US annexed their lan.pdf
What happened to Mexicans in the west after the US annexed their lan.pdf
 
What are pivot tablesWhat is the table format and how can it save.pdf
What are pivot tablesWhat is the table format and how can it save.pdfWhat are pivot tablesWhat is the table format and how can it save.pdf
What are pivot tablesWhat is the table format and how can it save.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 

In Java code, implement the HeapSort algorithm. Your function must t.pdf

  • 1. In Java code, implement the HeapSort algorithm. Your function must take an ArrayList as input and output a sorted ArrayList. Allow the program to accept user input that becomes added to the ArrayList, if no elements are added by the user, handle the exception. Solution Hi, Please find my program. Please let me know in case of any issue. import java.util.ArrayList; import java.util.Scanner; //Java program for implementation of Heap Sort public class HeapSort { public void sort(ArrayList list) { int n = list.size(); // Build heap (relistangelistay) for (int i = n / 2 - 1; i >= 0; i--) heapify(list, n, i); // One by one extract an element from heap for (int i=n-1; i>=0; i--) { // Move current root to end int temp = list.get(0); list.set(0, list.get(i)); list.set(i, temp); // call max heapify on the reduced heap heapify(list, i, 0); } } // To heapify a subtree rooted with node i which is // an index in list[]. n is size of heap void heapify(ArrayList list, int n, int i) { int largest = i; // Initialize largest as root
  • 2. int l = 2*i + 1; // left = 2*i + 1 int r = 2*i + 2; // right = 2*i + 2 // If left child is larger than root if (l < n && list.get(l) > list.get(largest)) largest = l; // If right child is larger than largest so far if (r < n && list.get(r) > list.get(largest)) largest = r; // If largest is not root if (largest != i) { int swap = list.get(i); list.set(i, list.get(largest)); list.set(largest, swap); // Recursively heapify the affected sub-tree heapify(list, n, largest); } } /* A utility function to print list of size n */ static void printlist(ArrayList list) { int n = list.size(); for (int i=0; i list = new ArrayList(); System.out.print("How many elements you want to sort: "); int n = sc.nextInt(); System.out.println("Enter "+n+" integers: "); for(int i=0; i