rename to distinguish btw number and word sorters
This commit is contained in:
40
Sorting/NumberCountingSorter.java
Normal file
40
Sorting/NumberCountingSorter.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package Sorting;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class NumberCountingSorter extends NumberSorter{
|
||||
|
||||
public NumberCountingSorter(RandomNumberFileReader reader){
|
||||
super("counting sort", reader);
|
||||
}
|
||||
|
||||
void sort(){
|
||||
if(numbers.length!=0){
|
||||
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;
|
||||
}
|
||||
}
|
||||
Integer[] countArray = new Integer[max-min+1];
|
||||
Arrays.fill(countArray, Integer.valueOf(0));
|
||||
for(int e: numbers){
|
||||
writeToArray(countArray, e-min, countArray[e-min]+1);
|
||||
|
||||
}
|
||||
int index=0;
|
||||
for(int i=0;i<countArray.length;i++){
|
||||
while(countArray[i]!=0){
|
||||
writeToArray(numbers, index, i+min);
|
||||
index++;
|
||||
writeToArray(countArray, i, countArray[i]-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user