use it if you need public class Patient implements Comparable<Patient>{ Q3. [15
ID: 3810024 • Letter: U
Question
use it if you need
public class Patient implements Comparable<Patient>{
Q3. [15 marksJ 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 Most questions are based on taken from the exercisesand examples listed in the textbook used by the course. 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 In order to properly compare the performance of the three sorting algorithms in Sorter, you need to have them work on three identical array lists. Start by creating an array list with random patients, then clone twice. Finally, sort each array list (original and clones) with a different algorithm and print the sorting time. Repeat this process for different values of N When you submit your code, you need submit two screen shots of the output of two runs of your code (similar to the ones given below)Explanation / Answer
Hi,
The code for each sort is given below. You just need to change the number of occurences and run again to get the table.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class SortTest {
private static ArrayList<String> RandomArray(int n)
{
ArrayList<String> arrayRandom = new ArrayList<String>(n);
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
for (int i=0; i<n; i++)
{
Integer r = rand.nextInt() % 50000;
arrayRandom.add("Patient"+r);
}
return arrayRandom;
}
private static void ShowArray(ArrayList<String> randomArray)
{
int n = randomArray.size();
ArrayList<String> showArray = new ArrayList<String>(n);
for (int i = 0; i<n; i++)
{
String myString = randomArray.get(i);
showArray.add(myString);
}
System.out.println(showArray);
}
public static void bubbleSort(String ar[])
{
ArrayList<String> myString = new ArrayList<String>();
for (int i = (ar.length - 1); i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (ar[j-1].compareToIgnoreCase(ar[j])>0)
{
String temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
} } }
myString = new ArrayList<String>(Arrays.asList(ar));
ShowArray(myString);
}
public static void selectionSort(String[] arr)
{
ArrayList<String> myString = new ArrayList<String>();
for (int i = 0; i < arr.length - 1; ++i)
{
int minIndex = i;
for (int j = i + 1; j < arr.length; ++j)
{
// "<" changed to use of compareTo()
if (arr[j].compareTo(arr[minIndex]) < 0)
{
minIndex = j;
}
}
// int changed to String
String temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
myString = new ArrayList<String>(Arrays.asList(arr));
ShowArray(myString);
}
public static void insertionSortAsc(String[] names, int i) {
ArrayList<String> myString = new ArrayList<String>();
String temp = names[i];
int j = i-1;
while (j >= 0 && names[j].compareToIgnoreCase(temp) > 0) {
names[j+1]=names[j];
j--;
}
names[j+1]=temp;
myString = new ArrayList<String>(Arrays.asList(names));
ShowArray(myString);
}
public static void main(String args[])
{
ArrayList<String> data = RandomArray(5000);
ShowArray(data);
String[] myArray = data.toArray(new String[0]);
long startTime = System.currentTimeMillis();
bubbleSort(myArray);
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
String[] myArray1 = data.toArray(new String[0]);
long startTime1 = System.currentTimeMillis();
selectionSort(myArray1);
long endTime1 = System.currentTimeMillis();
System.out.println(endTime1 - startTime1);
String[] myArray2 = data.toArray(new String[0]);
long startTime2 = System.currentTimeMillis();
insertionSortAsc(myArray2,4999);
long endTime2 = System.currentTimeMillis();
System.out.println(endTime2 - startTime2);
}
}
Run multiple times and you will get the table for each of the sorts. The first time for BubbleSort, Second One for SelectionSort and Third One for Insertion sort.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.