SlideShare a Scribd company logo
Sorting Questions (JAVA)
See attached classes below.
Attached Classes
-------------------------------------------
Patient.java
package A9.toStudents;
public class Patient implements Comparable{
//attributes
private int order;
private String name;
private boolean emergencyCase;
//constructor
public Patient(int order, String name, boolean emergencyCase) {
this.order = order;
this.name = name;
this.emergencyCase = emergencyCase;
}
//compareTo
public int compareTo(Patient other) {
if(this.isEmergencyCase() && !other.isEmergencyCase())
return -1; //place this first
else if(!this.isEmergencyCase() && other.isEmergencyCase())
return 1; //place other first
else //if both are emergency or both are not emergency
return this.getOrder()-other.getOrder(); //place smaller order first
}
//getters and setters
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEmergencyCase() {
return emergencyCase;
}
public void setEmergencyCase(boolean emergencyCase) {
this.emergencyCase = emergencyCase;
}
public String toString() {
return (isEmergencyCase()?"*":"") + name;
}
}
--------------------------------------------------------
PatientComparator.java
package A9.toStudents;
import java.util.Comparator;
public class PatientComparator implements Comparator{
public int compare(Patient p1, Patient p2) {
if(p1.isEmergencyCase() && !p2.isEmergencyCase())
return -1; //place p1 first
else if(!p1.isEmergencyCase() && p2.isEmergencyCase())
return 1; //place p2 first
else //if both are emergency or both are not emergency
return p1.getOrder()-p2.getOrder(); //place smaller order first
}
}
---------------------------------
PatientTestQ12.java
package A9.toStudents;
import java.util.ArrayList;
public class PatientTestQ12 {
public static void main(String[] args) {
ArrayList list = new ArrayList<>(5);
list.add(new Patient(1, "p1", false));
list.add(new Patient(2, "p2", false));
list.add(new Patient(3, "p3", true));
list.add(new Patient(4, "p4", false));
list.add(new Patient(5, "p5", true));
//before sorting
System.out.printf("%-15s%25s ", "Before sorting", list); //should be [p1, p2, p3, p4, p5]
//try bubble sort methods for Q1
//Sorter.bubbleSort(list);
//Sorter.bubbleSort(list, new PatientComparator());
//other sort methods for Q2
//Sorter.selectionSort(list);
//Sorter.insertionSort(list);
//after sorting
System.out.printf("%-15s%25s ", "After sorting", list); //should be [p3, p5, p1, p2, p4]
}
} In this assignment, you will build a class that can be used to sort a list of patient using
different algorithms, and you will also compare the time efficiency of these algorithms
Download the following files from Connect to help you work on this assignment: Patient.java
PatientComparator.java PatientTestQ12.java Q1. Create a class called Sorter that has two static
methods: (10 marks) public static void bubbleSort(ArrayList list) public static void bubble Sort
(ArrayList list, Comparator comparator) Write code for both methods so that they can sort an list
of items of the type Patient using bubble sort. The first one should use the Comparable interface
and the second uses the Comparator interface Test your code using the attached file
PatientTest012.java. You should get an output similar to the one given below for either method
Sample run (the asterisk indicates a patient with an emergency) Before sorting [pl, p2, *p3, p4,
*p5] After sorting [*p3, p5, p1, p2, p4] 02. Add the following two methods to your Sorter class.
The first method uses the selection sort algorithm and the second uses insertion sort. Both
methods should use the Comparable interface. (10 marks) public static void selection Sort
(ArrayList list) public static void insertionSort(Arrayist list) Test your code again using
PatientTest012.java. You should have the same output as in Q1 above Q3. (20 marks) Write a
program that obtains the execution time of the three sort algorithms used in the above Sorter
class (i.e., bubble sort, selection sort, and insertion sort) Your program should print out the time
required to sort array lists of N patients, where N ranges from 5,000 to 50,000 with an increment
of 5,000 (see sample run below). Every time increment N, your program should recreate
unsorted array lists of N random patients and then sort them. A random patient should have a
random id between 0 and N and random emergency case (true or false for emergencycase).
Don't worry much about creating a random name for each patient. Instead, use the name
“anonymous" (or any other name of your choice) for all patients
Solution
/**
* The java test program that calls the Sort class
* bubblesort,insertion sort and selection sort and print
* the Patient list to console.
* */
//PatientTestQ12.java
package A9.toStudents;
import java.util.ArrayList;
public class PatientTestQ12
{
public static void main(String[] args)
{
//Create a ArrayList of type Patient of size=5
ArrayList list = new ArrayList<>(5);
//create an instance of Patient to the list
list.add(new Patient(1, "p1", false));
list.add(new Patient(2, "p2", false));
list.add(new Patient(3, "p3", true));
list.add(new Patient(4, "p4", false));
list.add(new Patient(5, "p5", true));
//before sorting
System.out.printf("%-15s%25s ", "Before sorting", list);
System.out.println("Bubble sorting");
//try bubble sort methods for Q1
Sorter.bubbleSort(list);
System.out.println("Bubble sorting using PatientComparator");
Sorter.bubbleSort(list, new PatientComparator());
//other sort methods for Q2
System.out.println("Selection sorting");
Sorter.selectionSort(list);
System.out.println("Insertion sorting");
Sorter.insertionSort(list);
//after sorting
System.out.printf("%-15s%25s ", "After sorting", list); //should be [p3, p5, p1, p2, p4]
}
}
//Sorter class contains static methods that take ArrayList
//and sorts the list using bubble sort,insertion sort and
//selection sort
//Sorter.java
package A9.toStudents;
import java.util.ArrayList;
public class Sorter {
//Bubble sort
public static void bubbleSort(ArrayList list) {
for (int c = 0; c < ( list.size() - 1 ); c++)
{
for (int d = 0; d < list.size() - c - 1; d++)
{
if (list.get(d).compareTo(list.get(d+1))>0) /* For descending order use < */
{
Patient temp= list.get(d);
list.set(d, list.get(d+1));
list.set(d+1, temp);
}
}
}
}
//Selection sort
public static void selectionSort(ArrayList list) {
for (int i = 0; i < list.size() - 1; i++)
{
int index = i;
for (int j = i + 1; j < list.size(); j++)
if (list.get(j).compareTo(list.get(index))<0)
index = j;
Patient tempPatient = list.get(index);
list.set(index, list.get(i));
list.set(i,tempPatient);
}
}
//Insertion sort
public static void insertionSort(ArrayList list)
{
for (int i = 1; i < list.size(); i++)
{
for(int j = i ; j > 0 ; j--)
{
if(list.get(j).compareTo(list.get(j-1))<0)
{
Patient temp = list.get(j);
list.set(j,list.get(j-1));
list.set(j-1,temp);
}
}
}
}
public static void bubbleSort(ArrayList list,
PatientComparator patientComparator) {
for (int c = 0; c < ( list.size() - 1 ); c++)
{
for (int d = 0; d < list.size() - c - 1; d++)
{
if (patientComparator.compare(list.get(d),list.get(d+1))>0)
{
Patient temp= list.get(d);
list.set(d, list.get(d+1));
list.set(d+1, temp);
}
}
}
}
}
package A9.toStudents;
import java.util.Comparator;
public class PatientComparator implements Comparator{
public int compare(Patient p1, Patient p2) {
if(p1.isEmergencyCase() && !p2.isEmergencyCase())
return -1; //place p1 first
else if(!p1.isEmergencyCase() && p2.isEmergencyCase())
return 1; //place p2 first
else //if both are emergency or both are not emergency
return p1.getOrder()-p2.getOrder(); //place smaller order first
}
}
//Patient.java
package A9.toStudents;
public class Patient implements Comparable{
//attributes
private int order;
private String name;
private boolean emergencyCase;
//constructor
public Patient(int order, String name, boolean emergencyCase) {
this.order = order;
this.name = name;
this.emergencyCase = emergencyCase;
}
//compareTo
public int compareTo(Patient other) {
if(this.isEmergencyCase() && !other.isEmergencyCase())
return -1; //place this first
else if(!this.isEmergencyCase() && other.isEmergencyCase())
return 1; //place other first
else //if both are emergency or both are not emergency
return this.getOrder()-other.getOrder(); //place smaller order first
}
//getters and setters
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEmergencyCase() {
return emergencyCase;
}
public void setEmergencyCase(boolean emergencyCase) {
this.emergencyCase = emergencyCase;
}
public String toString() {
return (isEmergencyCase()?"*":"") + name;
}
}
--------------------------------------------------------------------------------
Sample output:
Before sorting [p1, p2, *p3, p4, *p5]
Bubble sorting
Bubble sorting using PatientComparator
Selection sorting
Insertion sorting
After sorting [*p3, *p5, p1, p2, p4]

More Related Content

PDF
Patient.java package A9.toStudents; public class Patient imple.pdf
PDF
Here is how the code works1. Patient.java is an Abstract Class whi.pdf
PDF
package s3; Copy paste this Java Template and save it as Emer.pdf
PDF
Define a class named Doctor whose objects are records for clinic’s d.pdf
PDF
Ive already completed the Java assignment below, but it doesnt wor.pdf
PDF
Objective Min-heap queue with customized comparatorHospital emerg.pdf
PDF
How to fix these erros The method sortListltTgt in the.pdf
PDF
import java.util.ArrayList; import java.util.List; import java.u.pdf
Patient.java package A9.toStudents; public class Patient imple.pdf
Here is how the code works1. Patient.java is an Abstract Class whi.pdf
package s3; Copy paste this Java Template and save it as Emer.pdf
Define a class named Doctor whose objects are records for clinic’s d.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdf
Objective Min-heap queue with customized comparatorHospital emerg.pdf
How to fix these erros The method sortListltTgt in the.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdf

Similar to Sorting Questions (JAVA)See attached classes below.Attached Clas.pdf (20)

PDF
I need help creating a parametized JUnit test case for the following.pdf
PPT
3 j unit
PDF
"Odisha's Living Legacy: Culture & Art".
DOCX
Junit With Eclipse
DOCX
Array list
PPT
Java Generics
PDF
Please create the appropriate JUnit test cases to thoroughly.pdf
PDF
import static org.junit.Assert.;import java.util.Arrays; import.pdf
PPSX
Java session4
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PDF
Java Generics - by Example
PDF
Java Generics - by Example
PDF
package singlylinkedlist; public class Node { public String valu.pdf
PPTX
Pragmatic unittestingwithj unit
PDF
Create a menu-driven program that will accept a collection of non-ne.pdf
PDF
Main class --------------------------import java.awt.FlowLayout.pdf
PDF
ReverseList.javaimport java.util.ArrayList;public class Rever.pdf
PDF
In Java- Create a Graduate class derived from Student- A graduate has.pdf
PPTX
Java Generics
I need help creating a parametized JUnit test case for the following.pdf
3 j unit
"Odisha's Living Legacy: Culture & Art".
Junit With Eclipse
Array list
Java Generics
Please create the appropriate JUnit test cases to thoroughly.pdf
import static org.junit.Assert.;import java.util.Arrays; import.pdf
Java session4
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Java Generics - by Example
Java Generics - by Example
package singlylinkedlist; public class Node { public String valu.pdf
Pragmatic unittestingwithj unit
Create a menu-driven program that will accept a collection of non-ne.pdf
Main class --------------------------import java.awt.FlowLayout.pdf
ReverseList.javaimport java.util.ArrayList;public class Rever.pdf
In Java- Create a Graduate class derived from Student- A graduate has.pdf
Java Generics

More from akritigallery (13)

PDF
some observations are above and some observation are below th.pdf
PDF
Some experts say that effective communication is the most important .pdf
PDF
Someone has a weighted coin that lands heads up with a probability o.pdf
PDF
Starz Department Store is located near the Towne Shopping Mall. At t.pdf
PDF
Some researchers have noted that adolescents who spend a lot of time.pdf
PDF
Star A has a parallax of .2 and star B has parallx .04. Star B a.pdf
PDF
Some organizations have formed crisis management teams comprised of .pdf
PDF
Stability of dividends is not important to stockholders.TrueFals.pdf
PDF
Stacia hikes at an average rate of 4 mi per hour.  The number of mil.pdf
PDF
Spread Spectrum TechniquesDescribe in detail a simple data communi.pdf
PDF
Specify what is included in the Money SupplySolutionAns .pdf
PDF
spaceSolution Lets say we have a vector sp.pdf
PDF
Sony PlayStation network hack in 2011PlayStation and Qriocity acco.pdf
some observations are above and some observation are below th.pdf
Some experts say that effective communication is the most important .pdf
Someone has a weighted coin that lands heads up with a probability o.pdf
Starz Department Store is located near the Towne Shopping Mall. At t.pdf
Some researchers have noted that adolescents who spend a lot of time.pdf
Star A has a parallax of .2 and star B has parallx .04. Star B a.pdf
Some organizations have formed crisis management teams comprised of .pdf
Stability of dividends is not important to stockholders.TrueFals.pdf
Stacia hikes at an average rate of 4 mi per hour.  The number of mil.pdf
Spread Spectrum TechniquesDescribe in detail a simple data communi.pdf
Specify what is included in the Money SupplySolutionAns .pdf
spaceSolution Lets say we have a vector sp.pdf
Sony PlayStation network hack in 2011PlayStation and Qriocity acco.pdf

Recently uploaded (20)

PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Classroom Observation Tools for Teachers
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Presentation on HIE in infants and its manifestations
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Classroom Observation Tools for Teachers
102 student loan defaulters named and shamed – Is someone you know on the list?
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
A systematic review of self-coping strategies used by university students to ...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
GDM (1) (1).pptx small presentation for students
Pharma ospi slides which help in ospi learning
Presentation on HIE in infants and its manifestations

Sorting Questions (JAVA)See attached classes below.Attached Clas.pdf

  • 1. Sorting Questions (JAVA) See attached classes below. Attached Classes ------------------------------------------- Patient.java package A9.toStudents; public class Patient implements Comparable{ //attributes private int order; private String name; private boolean emergencyCase; //constructor public Patient(int order, String name, boolean emergencyCase) { this.order = order; this.name = name; this.emergencyCase = emergencyCase; } //compareTo public int compareTo(Patient other) { if(this.isEmergencyCase() && !other.isEmergencyCase()) return -1; //place this first else if(!this.isEmergencyCase() && other.isEmergencyCase()) return 1; //place other first else //if both are emergency or both are not emergency return this.getOrder()-other.getOrder(); //place smaller order first } //getters and setters public int getOrder() { return order; } public void setOrder(int order) { this.order = order; } public String getName() {
  • 2. return name; } public void setName(String name) { this.name = name; } public boolean isEmergencyCase() { return emergencyCase; } public void setEmergencyCase(boolean emergencyCase) { this.emergencyCase = emergencyCase; } public String toString() { return (isEmergencyCase()?"*":"") + name; } } -------------------------------------------------------- PatientComparator.java package A9.toStudents; import java.util.Comparator; public class PatientComparator implements Comparator{ public int compare(Patient p1, Patient p2) { if(p1.isEmergencyCase() && !p2.isEmergencyCase()) return -1; //place p1 first else if(!p1.isEmergencyCase() && p2.isEmergencyCase()) return 1; //place p2 first else //if both are emergency or both are not emergency return p1.getOrder()-p2.getOrder(); //place smaller order first } } --------------------------------- PatientTestQ12.java package A9.toStudents; import java.util.ArrayList; public class PatientTestQ12 { public static void main(String[] args) { ArrayList list = new ArrayList<>(5);
  • 3. list.add(new Patient(1, "p1", false)); list.add(new Patient(2, "p2", false)); list.add(new Patient(3, "p3", true)); list.add(new Patient(4, "p4", false)); list.add(new Patient(5, "p5", true)); //before sorting System.out.printf("%-15s%25s ", "Before sorting", list); //should be [p1, p2, p3, p4, p5] //try bubble sort methods for Q1 //Sorter.bubbleSort(list); //Sorter.bubbleSort(list, new PatientComparator()); //other sort methods for Q2 //Sorter.selectionSort(list); //Sorter.insertionSort(list); //after sorting System.out.printf("%-15s%25s ", "After sorting", list); //should be [p3, p5, p1, p2, p4] } } In this assignment, you will build a class that can be used to sort a list of patient using different algorithms, and you will also compare the time efficiency of these algorithms Download the following files from Connect to help you work on this assignment: Patient.java PatientComparator.java PatientTestQ12.java Q1. Create a class called Sorter that has two static methods: (10 marks) public static void bubbleSort(ArrayList list) public static void bubble Sort (ArrayList list, Comparator comparator) Write code for both methods so that they can sort an list of items of the type Patient using bubble sort. The first one should use the Comparable interface and the second uses the Comparator interface Test your code using the attached file PatientTest012.java. You should get an output similar to the one given below for either method Sample run (the asterisk indicates a patient with an emergency) Before sorting [pl, p2, *p3, p4, *p5] After sorting [*p3, p5, p1, p2, p4] 02. Add the following two methods to your Sorter class. The first method uses the selection sort algorithm and the second uses insertion sort. Both methods should use the Comparable interface. (10 marks) public static void selection Sort (ArrayList list) public static void insertionSort(Arrayist list) Test your code again using PatientTest012.java. You should have the same output as in Q1 above Q3. (20 marks) Write a program that obtains the execution time of the three sort algorithms used in the above Sorter class (i.e., bubble sort, selection sort, and insertion sort) Your program should print out the time required to sort array lists of N patients, where N ranges from 5,000 to 50,000 with an increment
  • 4. of 5,000 (see sample run below). Every time increment N, your program should recreate unsorted array lists of N random patients and then sort them. A random patient should have a random id between 0 and N and random emergency case (true or false for emergencycase). Don't worry much about creating a random name for each patient. Instead, use the name “anonymous" (or any other name of your choice) for all patients Solution /** * The java test program that calls the Sort class * bubblesort,insertion sort and selection sort and print * the Patient list to console. * */ //PatientTestQ12.java package A9.toStudents; import java.util.ArrayList; public class PatientTestQ12 { public static void main(String[] args) { //Create a ArrayList of type Patient of size=5 ArrayList list = new ArrayList<>(5); //create an instance of Patient to the list list.add(new Patient(1, "p1", false)); list.add(new Patient(2, "p2", false)); list.add(new Patient(3, "p3", true)); list.add(new Patient(4, "p4", false)); list.add(new Patient(5, "p5", true)); //before sorting System.out.printf("%-15s%25s ", "Before sorting", list); System.out.println("Bubble sorting"); //try bubble sort methods for Q1 Sorter.bubbleSort(list); System.out.println("Bubble sorting using PatientComparator");
  • 5. Sorter.bubbleSort(list, new PatientComparator()); //other sort methods for Q2 System.out.println("Selection sorting"); Sorter.selectionSort(list); System.out.println("Insertion sorting"); Sorter.insertionSort(list); //after sorting System.out.printf("%-15s%25s ", "After sorting", list); //should be [p3, p5, p1, p2, p4] } } //Sorter class contains static methods that take ArrayList //and sorts the list using bubble sort,insertion sort and //selection sort //Sorter.java package A9.toStudents; import java.util.ArrayList; public class Sorter { //Bubble sort public static void bubbleSort(ArrayList list) { for (int c = 0; c < ( list.size() - 1 ); c++) { for (int d = 0; d < list.size() - c - 1; d++) { if (list.get(d).compareTo(list.get(d+1))>0) /* For descending order use < */ { Patient temp= list.get(d); list.set(d, list.get(d+1)); list.set(d+1, temp); } } } }
  • 6. //Selection sort public static void selectionSort(ArrayList list) { for (int i = 0; i < list.size() - 1; i++) { int index = i; for (int j = i + 1; j < list.size(); j++) if (list.get(j).compareTo(list.get(index))<0) index = j; Patient tempPatient = list.get(index); list.set(index, list.get(i)); list.set(i,tempPatient); } } //Insertion sort public static void insertionSort(ArrayList list) { for (int i = 1; i < list.size(); i++) { for(int j = i ; j > 0 ; j--) { if(list.get(j).compareTo(list.get(j-1))<0) { Patient temp = list.get(j); list.set(j,list.get(j-1)); list.set(j-1,temp); } } } } public static void bubbleSort(ArrayList list, PatientComparator patientComparator) { for (int c = 0; c < ( list.size() - 1 ); c++) { for (int d = 0; d < list.size() - c - 1; d++) { if (patientComparator.compare(list.get(d),list.get(d+1))>0)
  • 7. { Patient temp= list.get(d); list.set(d, list.get(d+1)); list.set(d+1, temp); } } } } } package A9.toStudents; import java.util.Comparator; public class PatientComparator implements Comparator{ public int compare(Patient p1, Patient p2) { if(p1.isEmergencyCase() && !p2.isEmergencyCase()) return -1; //place p1 first else if(!p1.isEmergencyCase() && p2.isEmergencyCase()) return 1; //place p2 first else //if both are emergency or both are not emergency return p1.getOrder()-p2.getOrder(); //place smaller order first } } //Patient.java package A9.toStudents; public class Patient implements Comparable{ //attributes private int order; private String name; private boolean emergencyCase; //constructor public Patient(int order, String name, boolean emergencyCase) { this.order = order; this.name = name; this.emergencyCase = emergencyCase; } //compareTo public int compareTo(Patient other) {
  • 8. if(this.isEmergencyCase() && !other.isEmergencyCase()) return -1; //place this first else if(!this.isEmergencyCase() && other.isEmergencyCase()) return 1; //place other first else //if both are emergency or both are not emergency return this.getOrder()-other.getOrder(); //place smaller order first } //getters and setters public int getOrder() { return order; } public void setOrder(int order) { this.order = order; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isEmergencyCase() { return emergencyCase; } public void setEmergencyCase(boolean emergencyCase) { this.emergencyCase = emergencyCase; } public String toString() { return (isEmergencyCase()?"*":"") + name; } } -------------------------------------------------------------------------------- Sample output: Before sorting [p1, p2, *p3, p4, *p5] Bubble sorting
  • 9. Bubble sorting using PatientComparator Selection sorting Insertion sorting After sorting [*p3, *p5, p1, p2, p4]