Added check that counting array isn't empty; avoids crash when file does not exist

This commit is contained in:
2020-06-01 18:10:19 -05:00
parent e21c430d42
commit 82ea963393

View File

@@ -7,26 +7,28 @@ public class CountingSorter extends Sorter{
}
void sort(){
int min = numbers[0];
int max = numbers[0];
for(int e: numbers){
if(compare(e,min)<0){
min=e;
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;
}
}
if(compare(e,max)>0){
max=e;
int[] countArray = new int[max-min+1];
for(int e: numbers){
countArray[e-min]++;
}
}
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]--;
int index=0;
for(int i=0;i<countArray.length;i++){
while(countArray[i]!=0){
numbers[index]=i+min;
index++;
countArray[i]--;
}
}
}
}