Design and implement a program that will compare selection sort, insertion sort,
ID: 3724340 • Letter: D
Question
Design and implement a program that will compare selection sort, insertion sort,
shell sort, merge sort, and quick sort. See note below.
You will need to meet the following specifications:
Your program will read in words from an external file and
store them in a simple object named scrabbleWord. The scrabbleWord class
should implement the Comparable class.
*scrabbleWord objects will be compared based on an integer score
representing their Scrabble score. For reference see:
http://www.scrabblepages.com/scrabble/rules/
For simplicity, non-standard characters (e.g. 'ç', 'è') will simply count 0.
*scrabbleWord objects should be stored in a Java ArrayList. Proper Java
Generics should be used.
*Your project should implement a JUnit test for each sort method. Each sort
method should sort the unsorted list, then call a single verification method to
confirm the list is sorted.
Your program will count swaps and compares for each run of each sort, and
report these values to compare the swap algorithms.
Explanation / Answer
import java.util.*;
import java.io.*;
public class P5Sort {
private ArrayList<Integer> listInt; // List of integer numbers
private ArrayList<String> stringList; //List of String words
private String fileName; // File name for the Output file
private String outputString; // Output String
private String outputFile; // File name for the Output file
private final String LS = System.lineSeparator();
private Scanner scan;
private long startSystemTime; // CPU clock startindg time for int
private long endSystemTime; // CPU clock starting time for float
private long startTime; // Wall clock starting time for int
private long endTime;
private double cpuTimeSelectionSort;
private double cpuTimeBubbleSort;
private double cpuTimeCollection;
private double cpuTimeSelectionSortInt;
private double cpuTimeBubbleSortInt;
private double cpuTimeCollectionInt;
private double wallClockSelectionSort;
private double wallClockBubbleSort;
private double wallClockCollectionSort;
private double wallClockSelectionSortInt;
private double wallClockBubbleSortInt;
private double wallClockCollectionSortInt;
public P5Sort() {
listInt = new ArrayList<Integer>();
stringList = new ArrayList<String>();
fileName = null;
outputFile = "P5Output.txt";
scan = new Scanner(System.in);
}
public static void main(String[] args) {
P5Sort sort = new P5Sort();
sort.timingSots();
}
public void timingSots() {
System.out.println("Please enter a valid Input file name that have Words and also with .txt ... ");
fileName = scan.nextLine();
readTheFilesForString();
System.out.println("Please enter a valid Input file name that have Numbers and also with .txt ... ");
fileName = scan.nextLine();
readTheFilesForInt();
// Timing selction sort for an Arralist of String
startTime = System.currentTimeMillis();
startSystemTime = System.nanoTime();
selectionSort();
endTime = System.currentTimeMillis();
endSystemTime = System.nanoTime();
wallClockSelectionSort = (double) (endTime - startTime) / 1000.0;
cpuTimeSelectionSort = (double) (endSystemTime - startSystemTime) / 100000000.0;
// Timing bubble sort for an Arralist of Strings
startTime = System.currentTimeMillis();
startSystemTime = System.nanoTime();
bubbleSort();
endTime = System.currentTimeMillis();
endSystemTime = System.nanoTime();
wallClockBubbleSort = (double) (endTime - startTime) / 1000.0;
cpuTimeBubbleSort = (double) (endSystemTime - startSystemTime) / 1000000000.0;
// Timing collection sort in an Arraylist of Strings
startTime = System.currentTimeMillis();
startSystemTime = System.nanoTime();
collectionSortString();
endTime = System.currentTimeMillis();
endSystemTime = System.nanoTime();
wallClockCollectionSort = (double) (endTime - startTime) / 1000.0;
cpuTimeCollection = (double) (endSystemTime - startSystemTime) / 1000000000.0;
// Timing selction sort for an Arraylist of Integers
startTime = System.currentTimeMillis();
startSystemTime = System.nanoTime();
selectionSortInt();
endTime = System.currentTimeMillis();
endSystemTime = System.nanoTime();
wallClockSelectionSortInt = (double) (endTime - startTime) / 1000.0;
cpuTimeSelectionSortInt = (double) (endSystemTime - startSystemTime) / 1000000000.0;
// Timing bubble sort for an Arraylist of Integers
startTime = System.currentTimeMillis();
startSystemTime = System.nanoTime();
bubbleSortInt();
endTime = System.currentTimeMillis();
endSystemTime = System.nanoTime();
wallClockBubbleSortInt = (double) (endTime - startTime) / 1000.0;
cpuTimeBubbleSortInt = (double) (endSystemTime - startSystemTime) / 1000000000.0;
// Timing selection sort for an Arraylist of Integers
startTime = System.currentTimeMillis();
startSystemTime = System.nanoTime();
selectionSortInt();
endTime = System.currentTimeMillis();
endSystemTime = System.nanoTime();
wallClockSelectionSortInt = (double) (endTime - startTime) / 1000.0;
cpuTimeSelectionSortInt = (double) (endSystemTime - startSystemTime) / 1000000000.0;
// Timing collection sort for an Arraylist of Integers
startTime = System.currentTimeMillis();
startSystemTime = System.nanoTime();
collectionSort();
endTime = System.currentTimeMillis();
endSystemTime = System.nanoTime();
wallClockCollectionSortInt = (double) (endTime - startTime) / 1000.0;
cpuTimeCollectionInt = (double) (endSystemTime - startSystemTime) / 1000000000.0;
outputToFile();
}// end of mainMenu
public void selectionSort() {
int minValue = 0;
int startScan;
int index;
String temp;
for (startScan = 0; startScan < stringList.size(); startScan++) {
stringList.set(minValue, stringList.get(startScan));
for (index = startScan + 1; index < stringList.size(); index++) {
if ((stringList.get(index)).compareToIgnoreCase(stringList.get(startScan)) < 0) {
temp = stringList.get(startScan);
stringList.set(startScan, stringList.get(index));
stringList.set(index, temp);
}
}
//System.out.println(stringList.get(startScan));
}
}// end of selection sort
public void selectionSortInt() {
int minValue = 0;
int startScan;
int index;
int temp;
for (startScan = 0; startScan < listInt.size(); startScan++) {
listInt.set(minValue, listInt.get(startScan));
for (index = startScan + 1; index < listInt.size(); index++) {
if ((listInt.get(index)) < (listInt.get(startScan))) {
temp = listInt.get(startScan);
listInt.set(startScan, listInt.get(index));
listInt.set(index, temp);
}
}
//System.out.println(stringList.get(startScan));
}
}// end of selectionSortInt
public void bubbleSort() {
int startScan;
int index;
String temp;
for (startScan = 0; startScan < stringList.size(); startScan++) {
for (index = startScan + 1; index < stringList.size(); index++) {
if ((stringList.get(index)).compareToIgnoreCase(stringList.get(startScan)) < 0) {
temp = stringList.get(startScan);
stringList.set(startScan, stringList.get(index));
stringList.set(index, temp);
}
}
//System.out.println(stringList.get(startScan));
}
}// end of bubbleSort
public void bubbleSortInt() {
int startScan;
int index;
int temp;
for (startScan = 0; startScan < listInt.size(); startScan++) {
for (index = startScan + 1; index < listInt.size(); index++) {
if ((listInt.get(index)) < (listInt.get(startScan))) {
temp = listInt.get(startScan);
listInt.set(startScan, listInt.get(index));
listInt.set(index, temp);
}
}
//System.out.println(stringList.get(startScan));
}
}// end of bubbleSortInt
public void collectionSort() {
Collections.sort(listInt);
}
public void collectionSortString() {
Collections.sort(stringList);
}
public void readTheFilesForString() {
String content = new String();
File file = new File(fileName);
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()) {
content = sc.nextLine();
stringList.add(content);
}
sc.close();
} catch (FileNotFoundException fnf) {
System.out.println("File not found.");
System.exit(0);
}
catch (Exception e) {
System.out.println(" Program terminated Safely...");
}
}// end of readTheFilesForString
public void readTheFilesForInt() {
int integerContent = 0;
File file = new File(fileName);
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()) {
integerContent = sc.nextInt();
listInt.add(integerContent);
}
sc.close();
} catch (FileNotFoundException fnf) {
System.out.println("File not found.");
System.exit(0);
}
catch (Exception e) {
System.out.println(" Program terminated Safely...");
}
}// end of readTheFilesForInt
/**
* Prints the report with the data from the trial
*/
public void outputToFile() {
outputString = "There are " + stringList.size() + " words in the list" +
LS + "There are " + listInt.size() + " numbers in the list " +
LS + "Selection sort for Strings " + "- " + cpuTimeSelectionSort + " seconds CPU time. " +
LS + "Selection sort for Integers " + "- " + cpuTimeSelectionSortInt + " seconds CPU time. " +
LS + "Bubble sort for String " + "- " + cpuTimeBubbleSort + " seconds CPU time. " +
LS + "Bubble sort for Integers " + "- " + cpuTimeBubbleSortInt + " seconds CPU time. " +
LS + "Collection sort for Strings " + "- " + cpuTimeCollection + " seconds CPU time. " +
LS + "Collection sort Integers " + "- " + cpuTimeCollectionInt + " seconds CPU time. " +
LS + "Selection sort for Strings " + "- " + wallClockSelectionSort + " seconds wall clock " +
LS + "Selection sort for Integers " + "- " + wallClockSelectionSortInt + " seconds wall clock " +
LS + "Bubble sort for String " + "- " + wallClockBubbleSort + " seconds wall clock " +
LS + "Bubble sort for Integers " + "- " + wallClockBubbleSortInt + " seconds wall clock " +
LS + "Collection sort for Strings " + "- " + wallClockCollectionSort + " seconds wall clock " +
LS + "Collection sort Integers " + "- " + wallClockCollectionSortInt + " seconds wall clock "
+ LS + LS;
System.out.println(outputString);
writeReport(outputString);
}// end of outputToFile
/**
* Writes the report with the data from the trial
*/
private void writeReport(String reportString) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputFile), true));
writer.write(reportString);
writer.close();
} catch (Exception ex) {
System.out.println("try a different file name");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.