//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
}
} //end of the PatientComparator
----------------------------------------------------------------------------
/*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 ascending order*/
{
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.s.
Patient.java package A9.toStudents; public class Patient imple.pdf
1. //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) {
2. 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
}
} //end of the PatientComparator
----------------------------------------------------------------------------
/*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
3. {
//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 ascending order*/
{
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)
{
4. 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);
}
}
}
}
}
----------------------------------------------------------------------------
/**
* The java test program PatientTestQ3 that sorts the random
* list of patient objects of size from 5000 to 50000 using
* bubble, insertion and selection sort and print
* the time taken by each sort to console.
* */
5. //PatientTestQ3.java
package A9.toStudents;
import java.util.ArrayList;
public class PatientTestQ3
{
public static void main(String[] args)
{
long startTime, endTime;
ArrayList list1, list2, list3;
System.out.printf("%5s%10s%10s%10s ", "N", "Bubble", "Selection", "Insertion");
for (int n = 5000; n <= 50000; n += 5000)
{
// create three identical lists
list1 = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list1.add(randomPatient(n));
list2 = (ArrayList) list1.clone();
list3 = (ArrayList) list1.clone();
// sort list1 using bubble sort and report the time
startTime = System.currentTimeMillis();
Sorter.bubbleSort(list1);
endTime = System.currentTimeMillis();
System.out.printf("%5d%10.3f", n, (endTime - startTime) / 1000.0);
// sort list2 using selection sort and report the time
startTime = System.currentTimeMillis();
Sorter.selectionSort(list2);
endTime = System.currentTimeMillis();
System.out.printf("%10.3f", (endTime - startTime) / 1000.0);
// sort list3 using insertion sort and report the time
startTime = System.currentTimeMillis();
Sorter.insertionSort(list3);
endTime = System.currentTimeMillis();
System.out.printf("%10.3f ", (endTime - startTime) / 1000.0);
}
}
private static Patient randomPatient(int n) {
7. 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;
8. 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
}
} //end of the PatientComparator
----------------------------------------------------------------------------
/*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 ascending order*/
{
Patient temp= list.get(d);
list.set(d, list.get(d+1));
list.set(d+1, temp);
}
}
9. }
}
//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++)
10. {
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);
}
}
}
}
}
----------------------------------------------------------------------------
/**
* The java test program PatientTestQ3 that sorts the random
* list of patient objects of size from 5000 to 50000 using
* bubble, insertion and selection sort and print
* the time taken by each sort to console.
* */
//PatientTestQ3.java
package A9.toStudents;
import java.util.ArrayList;
public class PatientTestQ3
{
public static void main(String[] args)
{
long startTime, endTime;
ArrayList list1, list2, list3;
System.out.printf("%5s%10s%10s%10s ", "N", "Bubble", "Selection", "Insertion");
for (int n = 5000; n <= 50000; n += 5000)
{
// create three identical lists
list1 = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list1.add(randomPatient(n));
11. list2 = (ArrayList) list1.clone();
list3 = (ArrayList) list1.clone();
// sort list1 using bubble sort and report the time
startTime = System.currentTimeMillis();
Sorter.bubbleSort(list1);
endTime = System.currentTimeMillis();
System.out.printf("%5d%10.3f", n, (endTime - startTime) / 1000.0);
// sort list2 using selection sort and report the time
startTime = System.currentTimeMillis();
Sorter.selectionSort(list2);
endTime = System.currentTimeMillis();
System.out.printf("%10.3f", (endTime - startTime) / 1000.0);
// sort list3 using insertion sort and report the time
startTime = System.currentTimeMillis();
Sorter.insertionSort(list3);
endTime = System.currentTimeMillis();
System.out.printf("%10.3f ", (endTime - startTime) / 1000.0);
}
}
private static Patient randomPatient(int n) {
int id = (int) (Math.random() * n);
boolean emergency = (Math.random() > 0.5) ? true : false;
return new Patient(id, "p" + id, emergency);
}
}
----------------------------------------------------------------------------
Sample Output:
N Bubble Selection Insertion
5000 0.281 0.188 0.234
10000 1.109 0.719 0.891
15000 2.282 1.468 1.922
20000 5.219 2.938 3.906
25000 7.422 5.906 5.578
30000 10.343 6.703 9.813
35000 17.219 11.031 11.641
40000 16.484 10.187 13.704