package Sorting;

import java.util.Arrays;

public class CountingSorter extends Sorter{

    public CountingSorter(String filename){
        super("counting", filename);
    }

    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);
                }
            }
        }
    }
    
}