much implementation, ready for first test
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package Sorting;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.ArrayList;
|
||||
import java.util.InputMismatchException;
|
||||
//import java.time.temporal.ChronoUnit;
|
||||
/**
|
||||
* This class contains the main method and presents an interface for the user to
|
||||
* compare sorting algorithm efficiencies. The user will be able to generate files
|
||||
@@ -19,7 +21,7 @@ public class AlgorithmTester{
|
||||
System.out.println("Please select an option:");
|
||||
System.out.println("1) Make a file of random numbers");
|
||||
System.out.println("2) Sort a file of random numbers");
|
||||
System.out.println("0) Exit")
|
||||
System.out.println("0) Exit");
|
||||
try{
|
||||
input = sc.nextInt();
|
||||
//Using the new switch syntax introduced in JDK 13
|
||||
@@ -78,12 +80,14 @@ public class AlgorithmTester{
|
||||
private static void sortFile(){
|
||||
String filename;
|
||||
int input;
|
||||
boolean bubble, selection, insertion, quick, counting, radix, merge, ready;
|
||||
boolean bubble=false, selection=false, insertion=false;
|
||||
boolean quick=false, counting=false; //radix=false, merge=false;
|
||||
boolean ready=false;
|
||||
System.out.print("Please enter file name: ");
|
||||
filename = sc.nextLine();
|
||||
while(!ready){
|
||||
try{
|
||||
System.out.println("Enter the number of the algorithms you wish to use.")
|
||||
System.out.println("Enter the number of the algorithms you wish to use.");
|
||||
System.out.println("Enter 0 to start sorting.");
|
||||
if(bubble){
|
||||
System.out.print("*");
|
||||
@@ -115,11 +119,36 @@ public class AlgorithmTester{
|
||||
case 0 -> ready=true;
|
||||
default -> System.out.println("Invalid input");
|
||||
}
|
||||
|
||||
} catch(InputMismatchException ex){
|
||||
System.out.println("Invalid input");
|
||||
}
|
||||
|
||||
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
||||
if(bubble){
|
||||
var bubSorter = new BubbleSorter(filename);
|
||||
results.add(bubSorter.measuredSort());
|
||||
}
|
||||
if(selection){
|
||||
var selSorter = new SelectionSorter(filename);
|
||||
results.add(selSorter.measuredSort());
|
||||
}
|
||||
if(insertion){
|
||||
var inSorter = new InsertionSorter(filename);
|
||||
results.add(inSorter.measuredSort());
|
||||
}
|
||||
if(counting){
|
||||
var countSorter = new CountingSorter(filename);
|
||||
results.add(countSorter.measuredSort());
|
||||
}
|
||||
if(quick){
|
||||
var qSorter = new QuickSorter(filename);
|
||||
results.add(qSorter.measuredSort());
|
||||
}
|
||||
for(SortResult e: results){
|
||||
System.out.println(e.getSortType()+" sort took:");
|
||||
System.out.println(e.getComparisonsUsed()+" comparisons");
|
||||
System.out.println(e.getSwapsUsed()+" swaps");
|
||||
System.out.println(e.getTimeUsed().toMillis()+" milliseconds\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ public class BubbleSorter extends Sorter{
|
||||
//a class to sort arrays of numbers using bubble sort
|
||||
|
||||
public BubbleSorter(String filename){
|
||||
super(filename);
|
||||
super("bubble", filename);
|
||||
}
|
||||
|
||||
void sort(){
|
||||
|
||||
14
Sorting/CountingSorter.java
Normal file
14
Sorting/CountingSorter.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package Sorting;
|
||||
|
||||
public class CountingSorter extends Sorter{
|
||||
|
||||
public CountingSorter(String filename){
|
||||
super("counting", filename);
|
||||
System.out.println("This is stub code, to be removed");
|
||||
}
|
||||
|
||||
void sort(){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,55 @@
|
||||
package Sorting;
|
||||
|
||||
//import java.util.Arrays;
|
||||
//import java.util.List;
|
||||
|
||||
public class InsertionSorter extends Sorter{
|
||||
|
||||
public InsertionSorter(String filename){
|
||||
super("insertion", filename);
|
||||
}
|
||||
|
||||
void sort(){
|
||||
int insertionValue;
|
||||
for(int i=1;i<numbers.length;i++){
|
||||
insertionValue=numbers[i];
|
||||
insert(i, insertionValue);
|
||||
}
|
||||
}
|
||||
|
||||
private void insert(int index, int ins){
|
||||
for(int i=index-1;i>=0;i--){
|
||||
int comp = compare(ins,numbers[i]);
|
||||
if(comp<0){
|
||||
if(i==0){
|
||||
numbers[i+1]=numbers[i];
|
||||
numbers[i]=ins;
|
||||
}
|
||||
else{
|
||||
numbers[i+1]=numbers[i];
|
||||
}
|
||||
}
|
||||
else{
|
||||
numbers[i+1]=ins;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
*This method uses a List for simple insertion
|
||||
*But I want to go C-style and just do array manipulation
|
||||
void sort(){
|
||||
List<Integer> nums = Arrays.asList(numbers);
|
||||
for(int i=1;i<nums.size();i++){
|
||||
int j=0;
|
||||
while((j<i)&&(nums.get(i).compareTo(nums.get(j))>0)){
|
||||
j++;
|
||||
}
|
||||
if(j!=i){
|
||||
swapsUsed++;
|
||||
nums.add(j, nums.remove(i));
|
||||
}
|
||||
}
|
||||
nums.toArray(numbers);
|
||||
}*/
|
||||
|
||||
}
|
||||
14
Sorting/QuickSorter.java
Normal file
14
Sorting/QuickSorter.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package Sorting;
|
||||
|
||||
public class QuickSorter extends Sorter{
|
||||
|
||||
public QuickSorter(String filename){
|
||||
super("quick", filename);
|
||||
System.out.println("This is stub code, to be removed");
|
||||
}
|
||||
|
||||
void sort(){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,4 +2,20 @@ package Sorting;
|
||||
|
||||
public class SelectionSorter extends Sorter {
|
||||
//a class to sort an array of numbers using selection sort
|
||||
public SelectionSorter(String filename){
|
||||
super("selection", filename);
|
||||
}
|
||||
|
||||
void sort(){
|
||||
for(int i=0;i<numbers.length-1;i++){
|
||||
int currentMinIndex = i;
|
||||
for(int j=i+1;j<numbers.length;j++){
|
||||
int comp = compare(numbers[currentMinIndex],numbers[j]);
|
||||
if(comp>0){
|
||||
currentMinIndex = j;
|
||||
}
|
||||
}
|
||||
swap(numbers[i],numbers[currentMinIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,14 @@ import java.time.Duration;
|
||||
*/
|
||||
public class SortResult {
|
||||
|
||||
private int[] sortedArray;
|
||||
private String sortType;
|
||||
private Integer [] sortedArray;
|
||||
private String comparisonsUsed;
|
||||
private String swapsUsed;
|
||||
private Duration timeUsed;
|
||||
|
||||
public SortResult(int[] a, long c, long s, Duration t){
|
||||
public SortResult(String st, Integer[] a, long c, long s, Duration t){
|
||||
sortType = st;
|
||||
sortedArray = a;
|
||||
if(c==0){
|
||||
comparisonsUsed = "None";
|
||||
@@ -41,11 +43,32 @@ public class SortResult {
|
||||
timeUsed = t;
|
||||
}
|
||||
|
||||
public SortResult(int [] a, Duration t){
|
||||
public SortResult(String st, Integer[] a, Duration t){
|
||||
sortType = st;
|
||||
sortedArray = a;
|
||||
timeUsed = t;
|
||||
comparisonsUsed = "Unknown";
|
||||
swapsUsed = "Unknown";
|
||||
}
|
||||
|
||||
public String getSortType(){
|
||||
return sortType;
|
||||
}
|
||||
|
||||
public Integer[] getSortedArray(){
|
||||
return sortedArray;
|
||||
}
|
||||
|
||||
public Duration getTimeUsed(){
|
||||
return timeUsed;
|
||||
}
|
||||
|
||||
public String getComparisonsUsed(){
|
||||
return comparisonsUsed;
|
||||
}
|
||||
|
||||
public String getSwapsUsed(){
|
||||
return swapsUsed;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,11 +10,13 @@ import java.lang.Integer;
|
||||
*/
|
||||
abstract class Sorter {
|
||||
|
||||
protected String sortType;
|
||||
protected Integer[] numbers;
|
||||
protected long swapsUsed = 0;
|
||||
protected long comparisonsUsed = 0;
|
||||
|
||||
public Sorter(String filename){
|
||||
public Sorter(String st, String filename){
|
||||
sortType=st;
|
||||
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
|
||||
reader.getNumbers().toArray(numbers);
|
||||
}
|
||||
@@ -43,7 +45,7 @@ abstract class Sorter {
|
||||
this.sort();
|
||||
Instant end = Instant.now();
|
||||
Duration time = Duration.between(start,end);
|
||||
SortResult output = new SortResult(numbers, comparisonsUsed, swapsUsed, time);
|
||||
SortResult output = new SortResult(sortType, numbers, comparisonsUsed, swapsUsed, time);
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user