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]

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

  • 1.
    Sorting Questions (JAVA) Seeattached 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 voidsetName(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 (seesample 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()); //othersort 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 staticvoid 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 usingPatientComparator Selection sorting Insertion sorting After sorting [*p3, *p5, p1, p2, p4]