implemented counting sort

This commit is contained in:
2020-05-31 12:07:41 -05:00
parent 4574231dcb
commit 13b66741a5
3 changed files with 1025 additions and 5 deletions

1000
1kRand-100to100 Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -145,9 +145,9 @@ public class AlgorithmTester{
results.add(qSorter.measuredSort()); results.add(qSorter.measuredSort());
} }
for(SortResult e: results){ for(SortResult e: results){
//for(int i: e.getSortedArray()){ for(int i: e.getSortedArray()){
// System.out.print(i+" "); System.out.print(i+" ");
//} }
System.out.println(""); 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");

View File

@@ -4,11 +4,31 @@ public class CountingSorter extends Sorter{
public CountingSorter(String filename){ public CountingSorter(String filename){
super("counting", filename); super("counting", filename);
System.out.println("This is stub code, to be removed");
} }
void sort(){ void sort(){
int min = numbers[0];
int max = numbers[0];
for(int e: numbers){
if(compare(e,min)<0){
min=e;
}
if(compare(e,max)>0){
max=e;
}
}
int[] countArray = new int[max-min+1];
for(int e: numbers){
countArray[e-min]++;
}
int index=0;
for(int i=0;i<countArray.length;i++){
while(countArray[i]!=0){
numbers[index]=i+min;
index++;
countArray[i]--;
}
}
} }
} }