Eliminated swap counting, got bubble, selection, and insertion sort working

This commit is contained in:
2020-05-31 09:49:28 -05:00
parent c680ec7671
commit 4574231dcb
15 changed files with 1111303 additions and 75 deletions
+33
View File
@@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch) - Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Debug (Launch)-AlgorithmTester<Tutoring-APCompSci_dadcd0e8>",
"request": "launch",
"mainClass": "Sorting.AlgorithmTester",
"projectName": "Tutoring-APCompSci_dadcd0e8"
},
{
"type": "java",
"name": "Debug (Launch)-RandomNumberGenerator",
"request": "launch",
"mainClass": "RandomNumberGenerator"
},
{
"type": "java",
"name": "Debug (Launch)-SortingTest",
"request": "launch",
"mainClass": "SortingTest"
}
]
}
+100000
View File
File diff suppressed because it is too large Load Diff
+10
View File
@@ -0,0 +1,10 @@
782
324
629
637
644
385
980
711
806
575
+10000
View File
File diff suppressed because it is too large Load Diff
+1000000
View File
File diff suppressed because it is too large Load Diff
+1000
View File
File diff suppressed because it is too large Load Diff
+200
View File
@@ -0,0 +1,200 @@
6
7
7
4
6
3
5
8
6
2
6
4
9
5
8
4
8
6
9
9
8
9
9
7
5
3
6
4
3
5
2
9
6
5
7
7
8
9
5
7
1
5
1
8
5
3
9
6
6
6
5
2
4
5
5
5
9
6
8
6
2
1
9
1
6
1
7
1
1
9
7
4
5
1
4
1
7
2
6
1
3
4
5
5
1
8
6
5
7
4
1
3
8
8
9
3
1
3
4
7
5
7
6
3
4
2
6
4
9
7
9
4
5
1
9
5
2
2
4
3
3
6
7
1
3
6
7
6
7
4
5
7
7
4
4
9
7
1
3
7
1
9
8
9
1
1
7
5
9
8
8
2
4
9
4
9
4
4
7
1
4
9
4
9
3
4
5
5
4
8
8
2
4
6
5
1
8
3
9
5
2
3
7
7
7
6
7
4
9
8
3
9
4
3
8
9
5
9
9
9
+12 -8
View File
@@ -84,7 +84,7 @@ public class AlgorithmTester{
boolean quick=false, counting=false; //radix=false, merge=false; boolean quick=false, counting=false; //radix=false, merge=false;
boolean ready=false; boolean ready=false;
System.out.print("Please enter file name: "); System.out.print("Please enter file name: ");
filename = sc.nextLine(); filename = sc.next();
while(!ready){ while(!ready){
try{ 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.");
@@ -122,33 +122,37 @@ public class AlgorithmTester{
}catch(InputMismatchException ex){ }catch(InputMismatchException ex){
System.out.println("Invalid input"); System.out.println("Invalid input");
} }
}
ArrayList<SortResult> results = new ArrayList<SortResult>(); ArrayList<SortResult> results = new ArrayList<SortResult>();
if(bubble){ if(bubble){
var bubSorter = new BubbleSorter(filename); BubbleSorter bubSorter = new BubbleSorter(filename);
results.add(bubSorter.measuredSort()); results.add(bubSorter.measuredSort());
} }
if(selection){ if(selection){
var selSorter = new SelectionSorter(filename); SelectionSorter selSorter = new SelectionSorter(filename);
results.add(selSorter.measuredSort()); results.add(selSorter.measuredSort());
} }
if(insertion){ if(insertion){
var inSorter = new InsertionSorter(filename); InsertionSorter inSorter = new InsertionSorter(filename);
results.add(inSorter.measuredSort()); results.add(inSorter.measuredSort());
} }
if(counting){ if(counting){
var countSorter = new CountingSorter(filename); CountingSorter countSorter = new CountingSorter(filename);
results.add(countSorter.measuredSort()); results.add(countSorter.measuredSort());
} }
if(quick){ if(quick){
var qSorter = new QuickSorter(filename); QuickSorter qSorter = new QuickSorter(filename);
results.add(qSorter.measuredSort()); results.add(qSorter.measuredSort());
} }
for(SortResult e: results){ 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.getSortType()+" sort took:");
System.out.println(e.getComparisonsUsed()+" comparisons"); System.out.println(e.getComparisonsUsed()+" comparisons");
System.out.println(e.getSwapsUsed()+" swaps");
System.out.println(e.getTimeUsed().toMillis()+" milliseconds\n"); System.out.println(e.getTimeUsed().toMillis()+" milliseconds\n");
} }
}
} }
} }
+1 -1
View File
@@ -14,7 +14,7 @@ public class BubbleSorter extends Sorter{
usedSwap=false; usedSwap=false;
for(int i=0;i<numbers.length-1;i++){ for(int i=0;i<numbers.length-1;i++){
if(compare(numbers[i],numbers[i+1])>0){ if(compare(numbers[i],numbers[i+1])>0){
swap(numbers[i],numbers[i+1]); swap(i,i+1);
usedSwap=true; usedSwap=true;
} }
} }
+11 -22
View File
@@ -1,7 +1,7 @@
package Sorting; package Sorting;
//import java.util.Arrays; import java.util.Arrays;
//import java.util.List; import java.util.List;
public class InsertionSorter extends Sorter{ public class InsertionSorter extends Sorter{
@@ -13,30 +13,19 @@ public class InsertionSorter extends Sorter{
int insertionValue; int insertionValue;
for(int i=1;i<numbers.length;i++){ for(int i=1;i<numbers.length;i++){
insertionValue=numbers[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 //This method uses a List for simple insertion
*But I want to go C-style and just do array manipulation //But I want to go C-style and just do array manipulation
void sort(){ void sort(){
List<Integer> nums = Arrays.asList(numbers); List<Integer> nums = Arrays.asList(numbers);
for(int i=1;i<nums.size();i++){ for(int i=1;i<nums.size();i++){
+1 -1
View File
@@ -34,7 +34,7 @@ public class RandomNumberFileMaker {
FileWriter randomWriter = new FileWriter(randomNumbers); FileWriter randomWriter = new FileWriter(randomNumbers);
for(int i=0;i<count;i++){ for(int i=0;i<count;i++){
int rand = min + (int) (Math.random()*(max-min)); int rand = min + (int) (Math.random()*(max-min));
randomWriter.write(rand); randomWriter.write(String.valueOf(rand));
randomWriter.write("\n"); randomWriter.write("\n");
} }
randomWriter.close(); randomWriter.close();
+2 -2
View File
@@ -16,8 +16,8 @@ class RandomNumberFileReader{
try{ try{
BufferedReader reader = Files.newBufferedReader(Path.of(file)); BufferedReader reader = Files.newBufferedReader(Path.of(file));
String line = null; String line = null;
while((line = String.valueOf(reader.readLine()))!=null){ while((line = reader.readLine())!=null){
numbers.add(Integer.valueOf(line)); numbers.add(Integer.parseInt(line));
} }
reader.close(); reader.close();
} catch (IOException ex){ } catch (IOException ex){
+1 -1
View File
@@ -15,7 +15,7 @@ public class SelectionSorter extends Sorter {
currentMinIndex = j; currentMinIndex = j;
} }
} }
swap(numbers[i],numbers[currentMinIndex]); swap(i,currentMinIndex);
} }
} }
} }
+6 -13
View File
@@ -22,24 +22,18 @@ public class SortResult {
private String sortType; private String sortType;
private Integer [] sortedArray; private Integer [] sortedArray;
private String comparisonsUsed; private String comparisonsUsed;
private String swapsUsed; // private String swapsUsed;
private Duration timeUsed; 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; sortType = st;
sortedArray = a; sortedArray = a;
if(c==0){ if(c==0){
comparisonsUsed = "None"; comparisonsUsed = "No";
} }
else{ else{
comparisonsUsed = Long.toString(c); comparisonsUsed = Long.toString(c);
} }
if(s==0){
swapsUsed = "None";
}
else{
swapsUsed = Long.toString(s);
}
timeUsed = t; timeUsed = t;
} }
@@ -48,7 +42,6 @@ public class SortResult {
sortedArray = a; sortedArray = a;
timeUsed = t; timeUsed = t;
comparisonsUsed = "Unknown"; comparisonsUsed = "Unknown";
swapsUsed = "Unknown";
} }
public String getSortType(){ public String getSortType(){
@@ -67,8 +60,8 @@ public class SortResult {
return comparisonsUsed; return comparisonsUsed;
} }
public String getSwapsUsed(){ //public String getSwapsUsed(){
return swapsUsed; //return swapsUsed;
} //}
} }
+5 -6
View File
@@ -12,12 +12,12 @@ abstract class Sorter {
protected String sortType; protected String sortType;
protected Integer[] numbers; protected Integer[] numbers;
protected long swapsUsed = 0;
protected long comparisonsUsed = 0; protected long comparisonsUsed = 0;
public Sorter(String st, String filename){ public Sorter(String st, String filename){
sortType=st; sortType=st;
RandomNumberFileReader reader = new RandomNumberFileReader(filename); RandomNumberFileReader reader = new RandomNumberFileReader(filename);
numbers = new Integer[reader.getNumbers().size()];
reader.getNumbers().toArray(numbers); reader.getNumbers().toArray(numbers);
} }
@@ -29,10 +29,9 @@ abstract class Sorter {
abstract void sort(); abstract void sort();
void swap(int a, int b){ void swap(int a, int b){
swapsUsed++; int tmp = numbers[a];
int tmp = a; numbers[a] = numbers[b];
a = b; numbers[b] = tmp;
b = tmp;
} }
int compare(int a, int b){ int compare(int a, int b){
@@ -45,7 +44,7 @@ abstract class Sorter {
this.sort(); this.sort();
Instant end = Instant.now(); Instant end = Instant.now();
Duration time = Duration.between(start,end); 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; return output;
} }