added code to count write operations used
This commit is contained in:
@@ -168,6 +168,7 @@ public class AlgorithmTester{
|
||||
}
|
||||
System.out.println("\n"+e.getSortType()+" sort took:");
|
||||
System.out.println(e.getComparisonsUsed()+" comparisons");
|
||||
System.out.println(e.getWritesUsed()+" write operations");
|
||||
System.out.println(e.getTimeUsed().toMillis()+" milliseconds");
|
||||
}
|
||||
|
||||
|
||||
@@ -21,13 +21,13 @@ public class CountingSorter extends Sorter{
|
||||
int[] countArray = new int[max-min+1];
|
||||
for(int e: numbers){
|
||||
countArray[e-min]++;
|
||||
wrtiesUsed++;
|
||||
writesUsed++;
|
||||
}
|
||||
int index=0;
|
||||
for(int i=0;i<countArray.length;i++){
|
||||
while(countArray[i]!=0){
|
||||
numbers[index]=i+min;
|
||||
wrtiesUsed++;
|
||||
writesUsed++;
|
||||
index++;
|
||||
countArray[i]--;
|
||||
}
|
||||
|
||||
@@ -16,9 +16,11 @@ public class InsertionSorter extends Sorter{
|
||||
int j=i-1;
|
||||
while(j>=0 && compare(numbers[j],insertionValue)>0){
|
||||
numbers[j+1]=numbers[j];
|
||||
writesUsed++;
|
||||
j--;
|
||||
}
|
||||
numbers[j+1]=insertionValue;
|
||||
writesUsed++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,30 +32,37 @@ public class MergeSorter extends Sorter{
|
||||
int[] right=new int[sizeRight];
|
||||
for(int i=0;i<sizeLeft;i++){
|
||||
left[i]=numbers[leftIndex+i];
|
||||
writesUsed++;
|
||||
}
|
||||
for(int i=0;i<sizeRight;i++){
|
||||
right[i]=numbers[middleIndex+1+i];
|
||||
writesUsed++;
|
||||
}
|
||||
int i=0, j=0;//indices of left and right array
|
||||
int k=leftIndex;//index of numbers array
|
||||
while(i<sizeLeft&&j<sizeRight){
|
||||
if(compare(left[i],right[j])<1){
|
||||
numbers[k]=left[i];
|
||||
writesUsed++;
|
||||
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
numbers[k]=right[j];
|
||||
writesUsed++;
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<sizeLeft){
|
||||
numbers[k]=left[i];
|
||||
writesUsed++;
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<sizeRight){
|
||||
numbers[k]=right[j];
|
||||
writesUsed++;
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ abstract class Sorter {
|
||||
protected String sortType;
|
||||
protected Integer[] numbers;
|
||||
protected long comparisonsUsed = 0;
|
||||
protected long wrtiesUsed=0;
|
||||
protected long writesUsed=0;
|
||||
|
||||
public Sorter(String st, String filename){
|
||||
sortType=st;
|
||||
@@ -33,7 +33,7 @@ abstract class Sorter {
|
||||
int tmp = numbers[a];
|
||||
numbers[a] = numbers[b];
|
||||
numbers[b] = tmp;
|
||||
wrtiesUsed+=2;
|
||||
writesUsed+=2;
|
||||
}
|
||||
|
||||
int compare(int a, int b){
|
||||
@@ -46,7 +46,7 @@ abstract class Sorter {
|
||||
this.sort();
|
||||
Instant end = Instant.now();
|
||||
Duration time = Duration.between(start,end);
|
||||
SortResult output = new SortResult(sortType, numbers, comparisonsUsed, time);
|
||||
SortResult output = new SortResult(sortType, numbers, comparisonsUsed, writesUsed, time);
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user