Need the codes for what\'s in bold PLEASE import java.util.*; import java.io.*;
ID: 3711307 • Letter: N
Question
Need the codes for what's in bold PLEASE
import java.util.*;
import java.io.*;
public class PersonSort
{
private static int numCompares = 0;
private static boolean printList = true;
public static void main (String[] args) throws Exception
{
// File name for testing the algorithms
final String PERSON_FILE32 = ".\src\Person32.txt";
// ArrayList size array for printing
String[] size = {"1K", "2K", "4K", "8K", "16K"};
// This set of files are for determining the running time of sorting randomized Persons
String[] random =
{".\src\Person1Ka.txt", ".\src\Person2Ka.txt", ".\src\Person4Ka.txt",
".\src\Person8Ka.txt", ".\src\Person16Ka.txt"};
// This set of files are for determining the running time of sorting sorted Persons
String[] sorted =
{".\src\Person1Kb.txt", ".\src\Person2Kb.txt", ".\src\Person4Kb.txt",
".\src\Person8Kb.txt", ".\src\Person16Kb.txt"};
// This set of files are for determining the running time of sorting sorted Persons
String[] revsorted =
{".\src\Person1Kc.txt", ".\src\Person2Kc.txt", ".\src\Person4Kc.txt",
".\src\Person8Kc.txt", ".\src\Person16Kc.txt"};
// Define an ArrayLst to hold the list of Persons
ArrayList<Person> personList = new ArrayList<Person>();
// Start sorting the small random file with each sort and print the results
String fileName = PERSON_FILE32;
System.out.println("Sorting the 32 item array using each of five algorithms");
System.out.println("======================================================");
System.out.println();
sort (personList, fileName);
printList = false;
// Sort randomly generated array list of Persons
for (int file = 0; file < 5; file++)
{
fileName = random[file];
System.out.println();
System.out.println ("ArrayLists with Persons Randomly Located");
System.out.println ("Sorting the " + size[file] + " item array using each of five algorithms");
System.out.println ("=======================================================");
System.out.println();
// Sort the list using 6 methods
sort (personList, fileName);
}
// Sort sorted array list of Persons
for (int file = 0; file < 5; file++)
{
fileName = sorted[file];
System.out.println();
System.out.println ("ArrayLists with Persons Sorted");
System.out.println ("Sorting the " + size[file] + " item array using each of five algorithms");
System.out.println ("=======================================================");
System.out.println();
// Sort the list using 6 methods
sort (personList, fileName);
}
// Sort reverse sorted array list of Persons
for (int file = 0; file < 5; file++)
{
fileName = revsorted[file];
System.out.println();
System.out.println ("ArrayLists with Persons Reverse Sorted");
System.out.println ("Sorting the " + size[file] + " item array using each of five algorithms");
System.out.println ("=======================================================");
System.out.println();
// Sort the list using 6 methods
sort (personList, fileName);
}
}
/**
* Populates a list of Persons from a file
*
* @param list
*/
public static void populate (ArrayList<Person> list, String fileName) throws Exception
{
list.clear();
numCompares = 0;
File file = new File (fileName);
Scanner fin = new Scanner (file);
String name = "";
int month, day, year;
while (fin.hasNext())
{
name = fin.next();
month = fin.nextInt();
day = fin.nextInt();
year = fin.nextInt();
list.add (new Person (name, year, month, day));
}
fin.close();
}
/**
* Prints the list of Persons
*
* @param list
*/
public static void print (ArrayList<Person> list)
{
if (printList)
{
for (Person p : list)
{
System.out.println (p.toString() + " ");
}
System.out.println ();
}
}
/**
* Calls all five types of sorts
*
* @param list
*/
public static void sort (ArrayList<Person> list, String fileName) throws Exception
{
// Call selection sort
populate (list, fileName);
selectionSort(list);
System.out.println ("Selection Sort");
System.out.println ("The number of compares: " + numCompares + " ");
print (list);
// Call insertion sort
populate (list, fileName);
insertionSort(list);
System.out.println ("Insertion Sort");
System.out.println ("The number of compares: " + numCompares + " ");
print (list);
// Call quick sort
populate (list, fileName);
quickSort(list);
System.out.println("Quick Sort");
System.out.println("The number of compares: " + numCompares + " ");
print(list);
// Call merge sort
populate (list, fileName);
mergeSort(list);
System.out.println ("Merge Sort");
System.out.println ("The number of compares: " + numCompares + " ");
print (list);
// Call heap sort
populate (list, fileName);
heapSort(list);
System.out.println("Heap Sort");
System.out.println ("The number of compares: " + numCompares + " ");
print (list);
}
/**
* Sorts a list of Persons using selection sort
*
* @param list
*/
public static void selectionSort (ArrayList<Person> list)
{
// Provide this code from Lab3: add in numCompares
}
/**
* Sorts a list of Persons using insertion sort
*
* @param list
*/
public static void insertionSort (ArrayList<Person> list)
{
// Provide this code from Lab3: add in numCompares
}
/**
* Sorts a list of Persons using merge sort
*
* @param list
*/
public static void mergeSort (ArrayList<Person> list)
{
// Provide this code
}
/**
* Merge two sorted lists
*
* @param list1
* @param list2
* @param temp
*/
public static void merge (ArrayList<Person> list1, ArrayList<Person> list2, ArrayList<Person> temp)
{
// Provide this code and add in numCompares
}
/**
* QuickSort start method
*
* @param list
*/
public static void quickSort(ArrayList<Person> list)
{
quickSort(list, 0, list.size() - 1);
}
/**
* QuickSort recursive method
*
* @param list
* @param start
* @param end
*/
private static void quickSort (ArrayList<Person> list, int start, int end)
{
// Provide this code
}
/**
* Partition the array list[start..lend]
*
* @param list
* @param start
* @param end
*/
private static int partition (ArrayList<Person> list, int start, int end)
{
// Provide this code and add in numCompares
}
/**
* Heap sort method
*
* @param list
*/
public static void heapSort (ArrayList<Person> list)
{
Heap<Person> heap = new Heap<Person>();
// Add elements to the heap
for (int i = 0; i < list.size(); i++)
{
heap.add (list.get (i));
}
// Remove elements from the heap
for (int i = list.size() - 1; i >= 0; i--)
{
list.set (i, heap.remove());
}
numCompares = heap.getNumCompares();
}
}
Explanation / Answer
If you have any doubts, please give me comment...
import java.util.*;
import java.io.*;
public class PersonSort {
private static int numCompares = 0;
private static boolean printList = true;
public static void main(String[] args) throws Exception {
// File name for testing the algorithms
final String PERSON_FILE32 = ".\src\Person32.txt";
// ArrayList size array for printing
String[] size = { "1K", "2K", "4K", "8K", "16K" };
// This set of files are for determining the running time of sorting randomized Persons
String[] random = { ".\src\Person1Ka.txt", ".\src\Person2Ka.txt", ".\src\Person4Ka.txt",
".\src\Person8Ka.txt", ".\src\Person16Ka.txt" };
// This set of files are for determining the running time of sorting sorted Persons
String[] sorted = { ".\src\Person1Kb.txt", ".\src\Person2Kb.txt", ".\src\Person4Kb.txt",
".\src\Person8Kb.txt", ".\src\Person16Kb.txt" };
// This set of files are for determining the running time of sorting sorted Persons
String[] revsorted = { ".\src\Person1Kc.txt", ".\src\Person2Kc.txt", ".\src\Person4Kc.txt",
".\src\Person8Kc.txt", ".\src\Person16Kc.txt" };
// Define an ArrayLst to hold the list of Persons
ArrayList<Person> personList = new ArrayList<Person>();
// Start sorting the small random file with each sort and print the results
String fileName = PERSON_FILE32;
System.out.println("Sorting the 32 item array using each of five algorithms");
System.out.println("======================================================");
System.out.println();
sort(personList, fileName);
printList = false;
// Sort randomly generated array list of Persons
for (int file = 0; file < 5; file++) {
fileName = random[file];
System.out.println();
System.out.println("ArrayLists with Persons Randomly Located");
System.out.println("Sorting the " + size[file] + " item array using each of five algorithms");
System.out.println("=======================================================");
System.out.println();
// Sort the list using 6 methods
sort(personList, fileName);
}
// Sort sorted array list of Persons
for (int file = 0; file < 5; file++) {
fileName = sorted[file];
System.out.println();
System.out.println("ArrayLists with Persons Sorted");
System.out.println("Sorting the " + size[file] + " item array using each of five algorithms");
System.out.println("=======================================================");
System.out.println();
// Sort the list using 6 methods
sort(personList, fileName);
}
// Sort reverse sorted array list of Persons
for (int file = 0; file < 5; file++) {
fileName = revsorted[file];
System.out.println();
System.out.println("ArrayLists with Persons Reverse Sorted");
System.out.println("Sorting the " + size[file] + " item array using each of five algorithms");
System.out.println("=======================================================");
System.out.println();
// Sort the list using 6 methods
sort(personList, fileName);
}
}
/**
* Populates a list of Persons from a file
*
* @param list
*/
public static void populate(ArrayList<Person> list, String fileName) throws Exception {
list.clear();
numCompares = 0;
File file = new File(fileName);
Scanner fin = new Scanner(file);
String name = "";
int month, day, year;
while (fin.hasNext()) {
name = fin.next();
month = fin.nextInt();
day = fin.nextInt();
year = fin.nextInt();
list.add(new Person(name, year, month, day));
}
fin.close();
}
/**
* Prints the list of Persons
*
* @param list
*/
public static void print(ArrayList<Person> list) {
if (printList) {
for (Person p : list) {
System.out.println(p.toString() + " ");
}
System.out.println();
}
}
/**
* Calls all five types of sorts
*
* @param list
*/
public static void sort(ArrayList<Person> list, String fileName) throws Exception {
// Call selection sort
populate(list, fileName);
selectionSort(list);
System.out.println("Selection Sort");
System.out.println("The number of compares: " + numCompares + " ");
print(list);
// Call insertion sort
populate(list, fileName);
insertionSort(list);
System.out.println("Insertion Sort");
System.out.println("The number of compares: " + numCompares + " ");
print(list);
// Call quick sort
populate(list, fileName);
quickSort(list);
System.out.println("Quick Sort");
System.out.println("The number of compares: " + numCompares + " ");
print(list);
// Call merge sort
populate(list, fileName);
mergeSort(list);
System.out.println("Merge Sort");
System.out.println("The number of compares: " + numCompares + " ");
print(list);
// Call heap sort
populate(list, fileName);
heapSort(list);
System.out.println("Heap Sort");
System.out.println("The number of compares: " + numCompares + " ");
print(list);
}
/**
* Sorts a list of Persons using selection sort
*
* @param list
*/
public static void selectionSort(ArrayList<Person> list) {
// Provide this code from Lab3: add in numCompares
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
numCompares++;
if (list.get(i).name > list.get(i).name) {
Person p = list.get(i);
list.set(i, list.get(j));
list.set(j, p);
}
}
}
}
/**
* Sorts a list of Persons using insertion sort
*
* @param list
*/
public static void insertionSort(ArrayList<Person> list) {
// Provide this code from Lab3: add in numCompares
int n = list.length;
for (int i = 1; i < n; ++i) {
Person temp = list.get(i);
int j = i - 1;
numCompares++;
while (j >= 0 && list.get(j).name > temp.name) {
list.set(j + 1, list.get(j));
j = j - 1;
}
list.set(j + 1, temp);
}
}
/**
* Sorts a list of Persons using merge sort
*
* @param list
*/
public static void mergeSort(ArrayList<Person> list) {
// Provide this code
if (list == null) {
return;
}
if (list.size() > 1) {
int mid = list.size() / 2;
// Split left part
Person[] left = new Person[mid];
for (int i = 0; i < mid; i++) {
left[i] = list[i];
}
// Split right part
Person[] right = new Person[list.size() - mid];
for (int i = mid; i < list.size(); i++) {
right[i - mid] = list[i];
}
mergeSort(left);
mergeSort(right);
int i = 0;
int j = 0;
int k = 0;
// Merge left and right arrays
while (i < left.size() && j < right.size()) {
numCompares++;
if (left[i].name < right[j].name) {
list[k] = left[i];
i++;
} else {
list[k] = right[j];
j++;
}
k++;
}
// Collect remaining elements
while (i < left.size()) {
list[k] = left[i];
i++;
k++;
}
while (j < right.length) {
list[k] = right[j];
j++;
k++;
}
}
}
/**
* Merge two sorted lists
*
* @param list1
* @param list2
* @param temp
*/
public static void merge(ArrayList<Person> list1, ArrayList<Person> list2, ArrayList<Person> temp) {
// Provide this code and add in numCompares
int i = 0, j = 0, k = 0, n1 = list1.size(), n2 = list2.size();
// Traverse both array
while (i < n1 && j < n2) {
if (list1.get(i) < list2.get(j))
temp.set(k++, list1.get(i++));
else
temp.set(k++, list2.get(j++));
}
// Store remaining elements of first array
while (i < n1)
temp.set(k++, list1.get(i++));
// Store remaining elements of second array
while (j < n2)
temp.get(k++, list2.get(j++));
}
/**
* QuickSort start method
*
* @param list
*/
public static void quickSort(ArrayList<Person> list) {
quickSort(list, 0, list.size() - 1);
}
/**
* QuickSort recursive method
*
* @param list
* @param start
* @param end
*/
private static void quickSort(ArrayList<Person> list, int start, int end) {
numCompares++;
if (start < end) {
int pi = partition(list, start, end);
sort(list, start, pi - 1);
sort(list, pi + 1, end);
}
}
/**
* Partition the array list[start..lend]
*
* @param list
* @param start
* @param end
*/
private static int partition(ArrayList<Person> list, int start, int end) {
Person pivot = list.get(end);
int i = (start - 1); // index of smaller element
for (int j = start; j < end; j++) {
numCompares++;
if (list.get(j).name <= pivot.name) {
i++;
Person temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
// swap arr[i+1] and arr[high] (or pivot)
Person temp = list.get(i + 1);
list.set(i + 1, list.get(end));
list.set(end, temp);
return i + 1;
}
/**
* Heap sort method
*
* @param list
*/
public static void heapSort(ArrayList<Person> list) {
Heap<Person> heap = new Heap<Person>();
// Add elements to the heap
for (int i = 0; i < list.size(); i++) {
heap.add(list.get(i));
}
// Remove elements from the heap
for (int i = list.size() - 1; i >= 0; i--) {
list.set(i, heap.remove());
}
numCompares = heap.getNumCompares();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.