Counting Sort 1

  • + 1 comment

    A simple solution using java

    public static List countingSort(List arr) { // Write your code here List count = new ArrayList<>();

    for(int i = 0; i < 100; i++) {
        count.add(0);    //Here we are setting the value of entire count array to 0.
    }
    for(int i = 0; i < arr.size(); i++) {
        int val = arr.get(i);    //taking out the value from arr index i.
        int currentcount = count.get(val); //then taking out the value from count at index val which gives us current count for that element val
        count.set(val, currentcount + 1); //incrementing the count array at index val by 1
    }
    return count;
    }
    

    }