implemented counting sort
This commit is contained in:
1000
1kRand-100to100
Normal file
1000
1kRand-100to100
Normal file
File diff suppressed because it is too large
Load Diff
@@ -145,9 +145,9 @@ public class AlgorithmTester{
|
||||
results.add(qSorter.measuredSort());
|
||||
}
|
||||
for(SortResult e: results){
|
||||
//for(int i: e.getSortedArray()){
|
||||
// System.out.print(i+" ");
|
||||
//}
|
||||
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");
|
||||
|
||||
@@ -4,11 +4,31 @@ public class CountingSorter extends Sorter{
|
||||
|
||||
public CountingSorter(String filename){
|
||||
super("counting", filename);
|
||||
System.out.println("This is stub code, to be removed");
|
||||
}
|
||||
|
||||
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]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user