Eliminated swap counting, got bubble, selection, and insertion sort working
This commit is contained in:
@@ -84,7 +84,7 @@ public class AlgorithmTester{
|
||||
boolean quick=false, counting=false; //radix=false, merge=false;
|
||||
boolean ready=false;
|
||||
System.out.print("Please enter file name: ");
|
||||
filename = sc.nextLine();
|
||||
filename = sc.next();
|
||||
while(!ready){
|
||||
try{
|
||||
System.out.println("Enter the number of the algorithms you wish to use.");
|
||||
@@ -119,36 +119,40 @@ public class AlgorithmTester{
|
||||
case 0 -> ready=true;
|
||||
default -> System.out.println("Invalid input");
|
||||
}
|
||||
} catch(InputMismatchException ex){
|
||||
}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");
|
||||
}
|
||||
}
|
||||
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
||||
if(bubble){
|
||||
BubbleSorter bubSorter = new BubbleSorter(filename);
|
||||
results.add(bubSorter.measuredSort());
|
||||
}
|
||||
if(selection){
|
||||
SelectionSorter selSorter = new SelectionSorter(filename);
|
||||
results.add(selSorter.measuredSort());
|
||||
}
|
||||
if(insertion){
|
||||
InsertionSorter inSorter = new InsertionSorter(filename);
|
||||
results.add(inSorter.measuredSort());
|
||||
}
|
||||
if(counting){
|
||||
CountingSorter countSorter = new CountingSorter(filename);
|
||||
results.add(countSorter.measuredSort());
|
||||
}
|
||||
if(quick){
|
||||
QuickSorter qSorter = new QuickSorter(filename);
|
||||
results.add(qSorter.measuredSort());
|
||||
}
|
||||
for(SortResult e: results){
|
||||
//for(int i: e.getSortedArray()){
|
||||
// System.out.print(i+" ");
|
||||
//}
|
||||
System.out.println("");
|
||||
System.out.println(e.getSortType()+" sort took:");
|
||||
System.out.println(e.getComparisonsUsed()+" comparisons");
|
||||
System.out.println(e.getTimeUsed().toMillis()+" milliseconds\n");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public class BubbleSorter extends Sorter{
|
||||
usedSwap=false;
|
||||
for(int i=0;i<numbers.length-1;i++){
|
||||
if(compare(numbers[i],numbers[i+1])>0){
|
||||
swap(numbers[i],numbers[i+1]);
|
||||
swap(i,i+1);
|
||||
usedSwap=true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package Sorting;
|
||||
|
||||
//import java.util.Arrays;
|
||||
//import java.util.List;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class InsertionSorter extends Sorter{
|
||||
|
||||
@@ -13,30 +13,19 @@ public class InsertionSorter extends Sorter{
|
||||
int insertionValue;
|
||||
for(int i=1;i<numbers.length;i++){
|
||||
insertionValue=numbers[i];
|
||||
insert(i, insertionValue);
|
||||
int j=i-1;
|
||||
while(j>=0 && compare(numbers[j],insertionValue)>0){
|
||||
numbers[j+1]=numbers[j];
|
||||
j--;
|
||||
}
|
||||
numbers[j+1]=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
|
||||
//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++){
|
||||
|
||||
@@ -34,7 +34,7 @@ public class RandomNumberFileMaker {
|
||||
FileWriter randomWriter = new FileWriter(randomNumbers);
|
||||
for(int i=0;i<count;i++){
|
||||
int rand = min + (int) (Math.random()*(max-min));
|
||||
randomWriter.write(rand);
|
||||
randomWriter.write(String.valueOf(rand));
|
||||
randomWriter.write("\n");
|
||||
}
|
||||
randomWriter.close();
|
||||
|
||||
@@ -16,8 +16,8 @@ class RandomNumberFileReader{
|
||||
try{
|
||||
BufferedReader reader = Files.newBufferedReader(Path.of(file));
|
||||
String line = null;
|
||||
while((line = String.valueOf(reader.readLine()))!=null){
|
||||
numbers.add(Integer.valueOf(line));
|
||||
while((line = reader.readLine())!=null){
|
||||
numbers.add(Integer.parseInt(line));
|
||||
}
|
||||
reader.close();
|
||||
} catch (IOException ex){
|
||||
|
||||
@@ -15,7 +15,7 @@ public class SelectionSorter extends Sorter {
|
||||
currentMinIndex = j;
|
||||
}
|
||||
}
|
||||
swap(numbers[i],numbers[currentMinIndex]);
|
||||
swap(i,currentMinIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,24 +22,18 @@ public class SortResult {
|
||||
private String sortType;
|
||||
private Integer [] sortedArray;
|
||||
private String comparisonsUsed;
|
||||
private String swapsUsed;
|
||||
// private String swapsUsed;
|
||||
private Duration timeUsed;
|
||||
|
||||
public SortResult(String st, Integer[] a, long c, long s, Duration t){
|
||||
public SortResult(String st, Integer[] a, long c, Duration t){
|
||||
sortType = st;
|
||||
sortedArray = a;
|
||||
if(c==0){
|
||||
comparisonsUsed = "None";
|
||||
comparisonsUsed = "No";
|
||||
}
|
||||
else{
|
||||
comparisonsUsed = Long.toString(c);
|
||||
}
|
||||
if(s==0){
|
||||
swapsUsed = "None";
|
||||
}
|
||||
else{
|
||||
swapsUsed = Long.toString(s);
|
||||
}
|
||||
timeUsed = t;
|
||||
}
|
||||
|
||||
@@ -48,7 +42,6 @@ public class SortResult {
|
||||
sortedArray = a;
|
||||
timeUsed = t;
|
||||
comparisonsUsed = "Unknown";
|
||||
swapsUsed = "Unknown";
|
||||
}
|
||||
|
||||
public String getSortType(){
|
||||
@@ -67,8 +60,8 @@ public class SortResult {
|
||||
return comparisonsUsed;
|
||||
}
|
||||
|
||||
public String getSwapsUsed(){
|
||||
return swapsUsed;
|
||||
}
|
||||
//public String getSwapsUsed(){
|
||||
//return swapsUsed;
|
||||
//}
|
||||
|
||||
}
|
||||
@@ -12,12 +12,12 @@ abstract class Sorter {
|
||||
|
||||
protected String sortType;
|
||||
protected Integer[] numbers;
|
||||
protected long swapsUsed = 0;
|
||||
protected long comparisonsUsed = 0;
|
||||
|
||||
public Sorter(String st, String filename){
|
||||
sortType=st;
|
||||
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
|
||||
numbers = new Integer[reader.getNumbers().size()];
|
||||
reader.getNumbers().toArray(numbers);
|
||||
}
|
||||
|
||||
@@ -29,10 +29,9 @@ abstract class Sorter {
|
||||
abstract void sort();
|
||||
|
||||
void swap(int a, int b){
|
||||
swapsUsed++;
|
||||
int tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
int tmp = numbers[a];
|
||||
numbers[a] = numbers[b];
|
||||
numbers[b] = tmp;
|
||||
}
|
||||
|
||||
int compare(int a, int b){
|
||||
@@ -45,7 +44,7 @@ abstract class Sorter {
|
||||
this.sort();
|
||||
Instant end = Instant.now();
|
||||
Duration time = Duration.between(start,end);
|
||||
SortResult output = new SortResult(sortType, numbers, comparisonsUsed, swapsUsed, time);
|
||||
SortResult output = new SortResult(sortType, numbers, comparisonsUsed, time);
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user