SlideShare a Scribd company logo
1 of 12
Download to read offline
//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.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.
* */
//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) {
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
45000 24.578 15.812 18.407
50000 25.782 16.812 21.406
Solution
//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.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.
* */
//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) {
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
45000 24.578 15.812 18.407
50000 25.782 16.812 21.406

More Related Content

Similar to Patient.java package A9.toStudents; public class Patient imple.pdf

Hello. Im creating a class called Bill. I need to design the class.pdf
Hello. Im creating a class called Bill. I need to design the class.pdfHello. Im creating a class called Bill. I need to design the class.pdf
Hello. Im creating a class called Bill. I need to design the class.pdfbarristeressaseren71
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfNicholasflqStewartl
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxwhitneyleman54422
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEDarwin Durand
 
ReverseList.javaimport java.util.ArrayList;public class Rever.pdf
 ReverseList.javaimport java.util.ArrayList;public class Rever.pdf ReverseList.javaimport java.util.ArrayList;public class Rever.pdf
ReverseList.javaimport java.util.ArrayList;public class Rever.pdfaryan9007
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfStewart29UReesa
 
Java 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR BeneluxJava 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR Beneluxyohanbeschi
 
public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfarjuncp10
 
i need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdfi need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdfirshadoptical
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monadJarek Ratajski
 
Logic Equations Resolver J Script
Logic Equations Resolver   J ScriptLogic Equations Resolver   J Script
Logic Equations Resolver J ScriptRoman Agaev
 
programming Code in C thatloops requesting information about patie.pdf
programming Code in C thatloops requesting information about patie.pdfprogramming Code in C thatloops requesting information about patie.pdf
programming Code in C thatloops requesting information about patie.pdfARCHANASTOREKOTA
 
Java Code for Sample Projects Inheritance
Java Code for Sample Projects InheritanceJava Code for Sample Projects Inheritance
Java Code for Sample Projects Inheritancejwjablonski
 
Ive already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdfIve already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdffantasiatheoutofthef
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdffootstatus
 
Objective Min-heap queue with customized comparatorHospital emerg.pdf
Objective Min-heap queue with customized comparatorHospital emerg.pdfObjective Min-heap queue with customized comparatorHospital emerg.pdf
Objective Min-heap queue with customized comparatorHospital emerg.pdfezhilvizhiyan
 
This is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdfThis is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdfakaluza07
 

Similar to Patient.java package A9.toStudents; public class Patient imple.pdf (20)

Hello. Im creating a class called Bill. I need to design the class.pdf
Hello. Im creating a class called Bill. I need to design the class.pdfHello. Im creating a class called Bill. I need to design the class.pdf
Hello. Im creating a class called Bill. I need to design the class.pdf
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
ReverseList.javaimport java.util.ArrayList;public class Rever.pdf
 ReverseList.javaimport java.util.ArrayList;public class Rever.pdf ReverseList.javaimport java.util.ArrayList;public class Rever.pdf
ReverseList.javaimport java.util.ArrayList;public class Rever.pdf
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 
New text document
New text documentNew text document
New text document
 
Java 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR BeneluxJava 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR Benelux
 
public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdf
 
i need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdfi need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdf
 
Miracle of std lib
Miracle of std libMiracle of std lib
Miracle of std lib
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monad
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Logic Equations Resolver J Script
Logic Equations Resolver   J ScriptLogic Equations Resolver   J Script
Logic Equations Resolver J Script
 
programming Code in C thatloops requesting information about patie.pdf
programming Code in C thatloops requesting information about patie.pdfprogramming Code in C thatloops requesting information about patie.pdf
programming Code in C thatloops requesting information about patie.pdf
 
Java Code for Sample Projects Inheritance
Java Code for Sample Projects InheritanceJava Code for Sample Projects Inheritance
Java Code for Sample Projects Inheritance
 
Ive already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdfIve already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdf
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
Objective Min-heap queue with customized comparatorHospital emerg.pdf
Objective Min-heap queue with customized comparatorHospital emerg.pdfObjective Min-heap queue with customized comparatorHospital emerg.pdf
Objective Min-heap queue with customized comparatorHospital emerg.pdf
 
This is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdfThis is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdf
 

More from DEEPAKSONI562

S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
 S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdfDEEPAKSONI562
 
The java program that prompts user to enter a string and .pdf
  The java program that prompts user to  enter a string and .pdf  The java program that prompts user to  enter a string and .pdf
The java program that prompts user to enter a string and .pdfDEEPAKSONI562
 
The answer is b, theres no increase in H. .pdf
                     The answer is b, theres no increase in H.        .pdf                     The answer is b, theres no increase in H.        .pdf
The answer is b, theres no increase in H. .pdfDEEPAKSONI562
 
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdf
                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdfDEEPAKSONI562
 
Natural rubber is a polymer of isoprene units ; e.pdf
                     Natural rubber is a polymer of isoprene units ; e.pdf                     Natural rubber is a polymer of isoprene units ; e.pdf
Natural rubber is a polymer of isoprene units ; e.pdfDEEPAKSONI562
 
no double displacement reactions can be mixing tw.pdf
                     no double displacement reactions can be mixing tw.pdf                     no double displacement reactions can be mixing tw.pdf
no double displacement reactions can be mixing tw.pdfDEEPAKSONI562
 
using System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdfusing System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdfDEEPAKSONI562
 
Toxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdfToxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdfDEEPAKSONI562
 
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdftime is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdfDEEPAKSONI562
 
This is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdfThis is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdfDEEPAKSONI562
 
Team public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdfTeam public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdfDEEPAKSONI562
 
PollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdfPollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdfDEEPAKSONI562
 
Non financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdfNon financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdfDEEPAKSONI562
 
C) III is correct note the configuration keep un.pdf
                     C) III is correct note the configuration keep un.pdf                     C) III is correct note the configuration keep un.pdf
C) III is correct note the configuration keep un.pdfDEEPAKSONI562
 
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdfLHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdfDEEPAKSONI562
 
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdfimport com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdfDEEPAKSONI562
 
intervalSolutioninterval.pdf
intervalSolutioninterval.pdfintervalSolutioninterval.pdf
intervalSolutioninterval.pdfDEEPAKSONI562
 
In2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdfIn2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdfDEEPAKSONI562
 
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdfFeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdfDEEPAKSONI562
 
D the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdfD the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdfDEEPAKSONI562
 

More from DEEPAKSONI562 (20)

S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
 S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
S = {0,1,2,3,4,5} P(S) be the set of all nonempty subsets of.pdf
 
The java program that prompts user to enter a string and .pdf
  The java program that prompts user to  enter a string and .pdf  The java program that prompts user to  enter a string and .pdf
The java program that prompts user to enter a string and .pdf
 
The answer is b, theres no increase in H. .pdf
                     The answer is b, theres no increase in H.        .pdf                     The answer is b, theres no increase in H.        .pdf
The answer is b, theres no increase in H. .pdf
 
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdf
                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf                     Step1 white ppt with NaCl(aq) indicates Ag+  Step.pdf
Step1 white ppt with NaCl(aq) indicates Ag+ Step.pdf
 
Natural rubber is a polymer of isoprene units ; e.pdf
                     Natural rubber is a polymer of isoprene units ; e.pdf                     Natural rubber is a polymer of isoprene units ; e.pdf
Natural rubber is a polymer of isoprene units ; e.pdf
 
no double displacement reactions can be mixing tw.pdf
                     no double displacement reactions can be mixing tw.pdf                     no double displacement reactions can be mixing tw.pdf
no double displacement reactions can be mixing tw.pdf
 
using System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdfusing System; using System.Collections.Generic; using System.Lin.pdf
using System; using System.Collections.Generic; using System.Lin.pdf
 
Toxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdfToxicants may move across biological membranes by all the following .pdf
Toxicants may move across biological membranes by all the following .pdf
 
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdftime is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
time is 18.2yearsdetailed answer i will submit nowSolutionti.pdf
 
This is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdfThis is the code for the above 5 public class Input extends JFram.pdf
This is the code for the above 5 public class Input extends JFram.pdf
 
Team public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdfTeam public class Team {    private String teamId;    priva.pdf
Team public class Team {    private String teamId;    priva.pdf
 
PollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdfPollinationFertilization1.     Pollination is the transfer of po.pdf
PollinationFertilization1.     Pollination is the transfer of po.pdf
 
Non financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdfNon financial performance indicators of United Utilities Group PLC. .pdf
Non financial performance indicators of United Utilities Group PLC. .pdf
 
C) III is correct note the configuration keep un.pdf
                     C) III is correct note the configuration keep un.pdf                     C) III is correct note the configuration keep un.pdf
C) III is correct note the configuration keep un.pdf
 
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdfLHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
LHS =2y+1RHS=-(-2y-1)         =2y+1LHS =RHSy=all the real nu.pdf
 
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdfimport com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
import com.rogue.roguer.command.CommandHandler; import com.rogue.r.pdf
 
intervalSolutioninterval.pdf
intervalSolutioninterval.pdfintervalSolutioninterval.pdf
intervalSolutioninterval.pdf
 
In2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdfIn2O3SolutionIn2O3.pdf
In2O3SolutionIn2O3.pdf
 
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdfFeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
FeCO3 is an ionic compound, composed of the Fe(II)2+ ion and the CO3.pdf
 
D the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdfD the polar covalent bond within methanol cannot form hydrogen bond .pdf
D the polar covalent bond within methanol cannot form hydrogen bond .pdf
 

Recently uploaded

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Ă...Nguyen Thanh Tu Collection
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesSHIVANANDaRV
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
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).pptxEsquimalt MFRC
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
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.pptxDr. Ravikiran H M Gowda
 
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 17Celine George
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
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.pptxCeline George
 

Recently uploaded (20)

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Ă...
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
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
 
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
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
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
 

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) {
  • 6. 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 45000 24.578 15.812 18.407 50000 25.782 16.812 21.406 Solution //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
  • 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
  • 12. 45000 24.578 15.812 18.407 50000 25.782 16.812 21.406